Skip to content

Instantly share code, notes, and snippets.

@Bolinha1
Last active December 18, 2015 23:19
Show Gist options
  • Select an option

  • Save Bolinha1/5860300 to your computer and use it in GitHub Desktop.

Select an option

Save Bolinha1/5860300 to your computer and use it in GitHub Desktop.
Classe upload, refatorada para fazer múltiplo upload.
<?php
namespace Upload;
class Upload
{
public $definePath = '';
private $path;
public function __construct($definePath, $path = 'arquivos/')
{
$this->path = dirname(__FILE__) . '/../../common/'.$path;
$this->definePath = $definePath;
}
public function upload($arq = 'arquivo')
{
$tmp = $_FILES[$arq]["tmp_name"];
$nameFile = $_FILES[$arq]["name"];
try
{
if(is_array($tmp) && is_array($nameFile))
{
for($i = 0; $i <= count($tmp) && $i <= count($nameFile); $i++)
{
if(!empty($tmp[$i]) && !empty($nameFile[$i]))
{
$rand = mt_rand(0, 99999);
$nameFile[] = str_replace(" ", "", $nameFile[$i]);
$nameFileModified[] = $rand.$nameFile[$i];
$place[] = $this->diretorio().$nameFileModified[$i]; // ../imagem/subPasta/nomeArquivo
move_uploaded_file($tmp[$i], $place[$i]);
$placeFinal[] = str_replace($this->path, '', $place[$i]);
}
}
}
return $placeFinal;
}
catch(Exception $e)
{
echo 'Houve um erro durante o upload.: ' .$e->getMessage();
}
}
public function diretorio()
{
if(!is_dir($this->path))
{
mkdir($this->path, 0777);
chmod($this->path, 0777);
}
$finalPath = $this->path.$this->definePath.'/';
if(is_dir($finalPath))
{
chmod($finalPath, 0777);
return $finalPath;
}
else
{
if(mkdir($finalPath, 0777))
{
chmod($finalPath, 0777);
return $finalPath;
}
}
}
}
<?php
$u = new Upload('teste');
$content = '<form action="" method="POST" enctype="multipart/form-data">
Send these files:<br />
<input name="arquivo[]" type="file" /><br />
<input name="arquivo[]" type="file" /><br />
<input name="arquivo[]" type="file" /><br />
<input type="submit" value="Send files" />
</form>';
echo $content;
var_dump($u->upload());
var_dump($u->diretorio());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment