<? class UpLoadAndChangeTheName { var $Archive; var $Diretorio = 'imgs/';//Defina o diretório das imagens var $Erro = array(); var $NomeGerado; function FormFile(){ echo '<form method="post" action="'.$PHP_SELF.'" enctype="multipart/form-data"><br> <table width="60%" border="0" align="center"> <tr align="center"> <td><strong>Arquivo - </strong></td> <td><input type="file" name="foto" size="40" id="but"/></td> <td><input type="submit" name="submit" value="Enviar!" id="but"/></td> </tr> </table> </form> '; } function CheckAction(){ if (empty($_POST['submit'])) { $this->FormFile(); }else{ $this->Archive = $_FILES['foto']; $this->UpLoadFile(); } } function UpLoadFile(){ $NomeDoArquivo = $this->Archive['name']; $ArquivoTemporario = $this->Archive['tmp_name']; $Tipo = $this->Archive['type']; if(ereg("(image)", $Tipo)){ move_uploaded_file($ArquivoTemporario, $this->Diretorio.$NomeDoArquivo); $this->RenameFile(); }else{ $this->Erro['AI'] .= 'Arquivo inválido!'; $this->Erro['AI'] .= '<strong> ['.$this->Archive['type'].']</strong><br />'; $this->Erro['AI'] .= 'Este sistema aceita apenas arquivos com MIME contendo \'image\''; } } function RenameFile(){ $NovoNome = md5(mktime()); $NomeDoArquivo = $this->Archive['name']; $Separa = explode('.', $NomeDoArquivo); $NewName = $NovoNome.'.'.$Separa[1]; $FileSize = round($this->Archive['size']/1024, 1); $Renomeia = rename($this->Diretorio.$NomeDoArquivo, $this->Diretorio.$NewName); if($Renomeia){ echo '<div align="left">O Arquivo <strong>'.$NomeDoArquivo.'</strong> foi enviado e renomeado para <strong><a href="'.$this->Diretorio.$NewName.'">'.$NewName.'</a></strong><br/><br/> <em>Informações do Arquivo...</em><br /> <strong>Tamanho:</strong> '.$FileSize.' Kb. </div>'; }else{ $this->Erro[] = 'Erro ao renomear Arquivo!'; } } function ShowErrors(){ foreach($this->Erro as $Erro){ echo '<strong>Houve um erro:</strong> '.$Erro; } } } $CallClass = new UpLoadAndChangeTheName; $CallClass->CheckAction(); $CallClass->ShowErrors(); ?>
Como usar?
Coloque ese arquivo em index.php e crie uma pasta chamada imgs
Além do famoso UpLoad ele faz mais alguma coisa?
Sim, ele verifica se é mesmo imagem por MIME, e renomeia a imagem mandada para um hash md5 para não correr o risco de erros e subscrição de imagens no servidor!