0

Classe para manipulação de uploads

criado por Raphael Basso em 21/01/2007 6:45am
<p>Como eu disse e todos vem dizendo, o uso de classes em seus códigos facilita um bocado a programação, pois há uma camada de abstração, o código não fica sujo, etc. Agora, vamos à um exemplo utiliando a classe criada anteriormente:</p>

<div style="background-color : #e0e0e0; width : 570px; padding : 4px;">
	<span style="color : #009900">&lt;!-- Formulário de Upload Usando a Classe Upload - index.php --&gt;</span><br /><br />

&lt;form action="upload.php" enctype="multipart/form-data" method="post"&gt;<br />
<br />
&nbsp; &nbsp;&lt;input type="hidden" name="MAX_FILE_SIZE" value="300000" /&gt;<br /><br />
&nbsp; &nbsp;&lt;input type="file" name="upload[]" /&gt;&lt;br /&gt;
<br /><br />

&nbsp; &nbsp;&lt;input type="file" name="upload[]" /&gt;&lt;br /&gt;
<br /><br />

&nbsp; &nbsp;&lt;input type="file" name="upload[]" /&gt;&lt;br /&gt;
<br /><br />

&nbsp; &nbsp;&lt;input type="file" name="upload[]" /&gt;&lt;br /&gt;
<br /><br />

&nbsp; &nbsp;&lt;input type="file" name="upload[]" /&gt;&lt;br /&gt;
<br /><br />

&nbsp; &nbsp;&lt;input type="file" name="upload[]" /&gt;&lt;br /&gt;
<br /><br />

&nbsp; &nbsp;&lt;input type="file" name="upload[]" /&gt;&lt;br /&gt;
<br /><br />

&nbsp; &nbsp;&lt;input type="file" name="upload[]" /&gt;&lt;br /&gt;
<br /><br />

&nbsp; &nbsp;&lt;input type="file" name="upload[]" /&gt;&lt;br /&gt;
<br /><br />

&nbsp; &nbsp;&lt;input type="file" name="upload[]" /&gt;&lt;br /&gt;
<br /><br />

&nbsp; &nbsp;&lt;input type="submit" value="Enviar" /&gt;&lt;br /&gt;<br />
&lt;/form&gt;
</div><br />

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

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

&nbsp; &nbsp;include "upload.class.php";<br /><br />

&nbsp; &nbsp;<span style="color : #009900">// Instânciando a classe </span><br /><br />

&nbsp; &nbsp;$up = new Upload('upload');<br /><br />

&nbsp; &nbsp;<span style="color : #009900">// Pegando todos os argumentos </span><br /><br />

&nbsp; &nbsp;$args = $up->args();<br /><br />

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

&nbsp; &nbsp;$destiny = "/var/www/upload/";<br /><br />

&nbsp; &nbsp;<span style="color : #009900">// Total enviados</span><br /><br />

&nbsp; &nbsp;$total = 0;<br /><br />

&nbsp; &nbsp;<span style="color : #009900">// Não enviados, fora de tamanho e parcialmente enviado</span><br /><br />

&nbsp; &nbsp;$nsp = 0;<br /><br />

&nbsp; &nbsp;foreach ($args as $f)<br />
&nbsp; &nbsp;{<br />

&nbsp; &nbsp;&nbsp; &nbsp;switch ($f->error)<br />
&nbsp; &nbsp;&nbsp; &nbsp;{<br />

&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;case UPLOAD_ERR_OK:<br />

&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;$f->move($destiny);<br /><br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;$total++;<br />

&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;break;<br /><br />

&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;default:<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;$nsp++;<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;break;<br />
&nbsp; &nbsp;&nbsp; &nbsp;}<br />
&nbsp; &nbsp;}<br /><br />

&nbsp; &nbsp;<span style="color : #0000ff">printf</span>("Total de arquivos 
enviados.: %d", $total);<br />

&nbsp; &nbsp;<span style="color : #0000ff">printf</span>("&lt;br /&gt;Arquivos com algum tipo de erro.: %d", $nsp);<br />
?&gt;
</div>

<p>ou</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">// Script Usando Classe Upload - upload.php </span><br /><br />

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

&nbsp; &nbsp;include "upload.class.php";<br /><br />

&nbsp; &nbsp;<span style="color : #009900">// Instânciando a classe </span><br /><br />

&nbsp; &nbsp;$up = new Upload('upload');<br /><br />

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

&nbsp; &nbsp;$destiny = "/var/www/upload/";<br /><br />

&nbsp; &nbsp;<span style="color : #009900">// Total enviados</span><br /><br />

&nbsp; &nbsp;$total = 0;<br /><br />

&nbsp; &nbsp;<span style="color : #009900">// Não enviados, fora de tamanho e parcialmente enviado</span><br /><br />

&nbsp; &nbsp;$nsp = 0;<br /><br />

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

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

&nbsp; &nbsp;&nbsp; &nbsp;$arg = $up->arg($i);<br /><br />

&nbsp; &nbsp;&nbsp; &nbsp;switch ($arg->error)<br />
&nbsp; &nbsp;&nbsp; &nbsp;{<br />

&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;<span style="color : #009900">// Arquivo OK </span><br /><br />

&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;case UPLOAD_ERR_OK:<br />

&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;$arg->move($destiny);<br /><br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;$total++;<br />

&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;break;<br /><br />

&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;<span style="color : #009900">// Arquivo com erro </span><br /><br />

&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;default:<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;$nsp++;<br />

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

&nbsp; &nbsp;<span style="color : #0000ff">printf</span>("Total de arquivos enviados.: %d", $total);<br />

&nbsp; &nbsp;<span style="color : #0000ff">printf</span>("&lt;br /&gt;Arquivos com algum tipo de erro.: %d", $nsp);<br />
?&gt;
</div>

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.)