0

Classe para manipulação de uploads

criado por Raphael Basso em 21/01/2007 6:45am
<p>Segue-se abaixo o código para PHP 5 como prometi. Não modifica muita coisa, somente o encapsulamento e a classe UploadArgs declarei como final, pois não precisa mais ser herdada. Outra coisa, apesar de você estar usando o PHP 5, o outro código também é para funcionar.
</p>

<div style="background-color : #e0e0e0; width : 560px; padding : 4px;">
&lt;?<span style="color : #ff0000">php</span><br /><br />
&nbsp; &nbsp;<span style="color : #009900">/********************/ </span><br />
&nbsp; &nbsp;<span style="color : #009900">/* Classe Upload &nbsp; &nbsp;*/ </span><br />
&nbsp; &nbsp;<span style="color : #009900">/* arabasso &nbsp; &nbsp; &nbsp; &nbsp; */ </span><br />
&nbsp; &nbsp;<span style="color : #009900">/* 2007 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; */ </span><br />
&nbsp; &nbsp;<span style="color : #009900">/********************/ </span><br /><br />

&nbsp; &nbsp;<span style="color : #009900">// Classe Para Manipulação de Uploads - upload.class.php </span><br /><br />
&nbsp; &nbsp;class Upload<br />
&nbsp; &nbsp;{<br />
&nbsp; &nbsp;&nbsp; &nbsp;private<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;$data;<br /><br />

&nbsp; &nbsp;&nbsp; &nbsp;public<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;$count;<br /><br />

&nbsp; &nbsp;&nbsp; &nbsp;<span style="color : #009900">// Construtor da classe Upload</span><br /><br />

<!-- Função Construtora -->

&nbsp; &nbsp;&nbsp; &nbsp;public function __construct($index)<br />
&nbsp; &nbsp;&nbsp; &nbsp;{<br />

&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;<span style="color : #009900">// Se o índice não for uma string</span><br /><br />

&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;if (!is_string($index))<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;
die("&lt;b&gt;Erro:&lt;/b&gt; STRING esperada!");<br /><br />

&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;<span style="color : #009900">// Se o array com este índice não existe</span><br /><br />

&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;if (!isset($_FILES[$index]))<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;
die("&lt;b&gt;Erro:&lt;/b&gt; ÍNDICE indefinido!");<br /><br />

&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;<span style="color : #009900">// Referência para a variável</span><br /><br />

&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;$this->data = & $_FILES[$index];<br /><br />

&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;<span style="color : #009900">// Número de elementos</span><br /><br />

&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;$this->count = count($this->data["name"]);<br />
&nbsp; &nbsp;&nbsp; &nbsp;}<br /><br />

<!-- Função Argumento 1 -->

&nbsp; &nbsp;&nbsp; &nbsp;<span style="color : #009900">// Pega um elemento baseado no índice</span><br /><br />

&nbsp; &nbsp;&nbsp; &nbsp;public function arg($index)<br />
&nbsp; &nbsp;&nbsp; &nbsp;{<br />

&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;<span style="color : #009900">// Se o índice for menor que 0 ou maior que o total</span><br /><br />

&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;if ($index < 0 || $index > $this->count)<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;
die("&lt;b&gt;Erro:&lt;/b&gt; ÍNDICE inv&aacute;lido!");<br /><br />

&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;<span style="color : #009900">// Retornando uma instância do objeto UploadArgs</span><br /><br />

&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;return new UploadArgs($this->data, $index);<br />
&nbsp; &nbsp;&nbsp; &nbsp;}<br /><br />

<!-- Função Argumentos Array -->

&nbsp; &nbsp;&nbsp; &nbsp;<span style="color : #009900">// Retorna um array de objetos</span><br /><br />

&nbsp; &nbsp;&nbsp; &nbsp;public function args()<br />
&nbsp; &nbsp;&nbsp; &nbsp;{<br />

&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;$arr = array();<br /><br />

&nbsp; &nbsp;&nbsp; &nbsp;<span style="color : #009900">// Adicionando instâncias ao array</span><br />

&nbsp; &nbsp;&nbsp; &nbsp;<span style="color : #009900">// array_push adiciona um elemento no final do array</span><br /><br />

&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;for ($i = 0; $i < $this->count; $i++)<br />

&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;array_push($arr, new UploadArgs($this->data, $i));<br /><br />

&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;return $arr;<br />

&nbsp; &nbsp;&nbsp; &nbsp;}<br />

&nbsp; &nbsp;}<br /><br />
&nbsp; &nbsp;<span style="color : #009900">// Classe para Informações do Arquivo Enviado </span><br /><br />
&nbsp; &nbsp;final class UploadArgs<br />
&nbsp; &nbsp;{<br />
&nbsp; &nbsp;&nbsp; &nbsp;public<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;$name, $size, $type, $tmpName, $error;<br /><br />

&nbsp; &nbsp;&nbsp; &nbsp;<span style="color : #009900">// Construtor da classe UploadArgs</span><br /><br />

<!-- Função Construtora -->

&nbsp; &nbsp;&nbsp; &nbsp;public function __construct(& $data, $index)<br />
&nbsp; &nbsp;&nbsp; &nbsp;{<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;$this->name &nbsp;&nbsp; = $data["name"][$index];<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;$this->size &nbsp;&nbsp; = $data["size"][$index];<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;$this->type &nbsp;&nbsp; = $data["type"][$index];<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;$this->tmpName = $data["tmp_name"][$index];<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;$this->error &nbsp; = $data["error"][$index];
<br />
&nbsp; &nbsp;&nbsp; &nbsp;}<br /><br />

<!-- Função Move o arquivo enviado -->

&nbsp; &nbsp;&nbsp; &nbsp;<span style="color : #009900">// Move o arquivo para a pasta destino</span><br /><br />

&nbsp; &nbsp;&nbsp; &nbsp;public function move($destiny)<br />
&nbsp; &nbsp;&nbsp; &nbsp;{<br />

&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;<span style="color : #009900">// Identificando o destino</span><br /><br />

&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;$destiny .= $this->name;<br /><br />

&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;<span style="color : #009900">// Movendo o arquivo</span><br /><br />

&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;move_uploaded_file($this->tmpName, $destiny);<br />

&nbsp; &nbsp;&nbsp; &nbsp;}<br /><br />
&nbsp; &nbsp;}<br />
?&gt;
</div><br />

Comentários:

Nenhum comentário foi enviado ainda.

Novo Comentário:

(Você pode usar tags como <b>, <i> ou <code>. URLs serão convertidas para links automaticamente.)