Upload de imagens

Publicado por adrianosalles (última atualização em 28/09/2009)

[ Hits: 6.812 ]

Download up.php




Script para fazer o upload de imagens com validações.

  



Esconder código-fonte

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<body>
   <?php
   if(isset($_POST["enviar"]))
      {
            $arquivo = $_FILES["imagem"]["name"];
         
         // verifica se o arquivo não tem caracteres especias
            $arquivo = str_replace("á","a",$arquivo);
            $arquivo = str_replace("â","a",$arquivo);
            $arquivo = str_replace("à","a",$arquivo);
            $arquivo = str_replace("ã","a",$arquivo);
            $arquivo = str_replace("é","e",$arquivo);
            $arquivo = str_replace("è","e",$arquivo);
            $arquivo = str_replace("ê","e",$arquivo);
            $arquivo = str_replace("ò","o",$arquivo);
            $arquivo = str_replace("ó","o",$arquivo);
            $arquivo = str_replace("ô","o",$arquivo);
            $arquivo = str_replace("õ","o",$arquivo);
            $arquivo = str_replace("í","i",$arquivo);
            $arquivo = str_replace("ì","i",$arquivo);
            $arquivo = str_replace(" ","_",$arquivo);
            
            //coloca o nome do arquivo todo em minúsculas
            $arquivo = strtolower($arquivo);
            
            
         //verifica se é um arquivo valido
         if(!eregi("^image\/(gif|jpg|jpeg|pjpg)",$_FILES["imagem"]["type"]))
            {
               print "<script>alert(\"nao e um arquivo valido\");</script>";   
            }
         else
            {
               //verifica se o nome do arquivo já existe
               if(file_exists($arquivo))
                  {
                     $a=1;
                     while(file_exists("[$a]$arquivo"))
                        {
                           $a++;   
                        }
                     $arquivo = "[$a]$arquivo";
                  }
               
               //verifica se o arquivo foi copiado com sucesso para o diretório
               if(!move_uploaded_file($_FILES["imagem"]["tmp_name"],$arquivo))
                  {
                     print "<script>alert(\"Falha ao carregar o arquivo\");</script>";   
                  }
               print "<script>alert(\"Arquivo carregado com sucesso\");</script>";
            }
      }
   
   ?>

<form action="" method="post" enctype="multipart/form-data" name="form1">
<table width="30%" border="0" align="center">
  <tr>
     
    
    <td>Enviar uma imagem:</td>
    <td>Sim <input name="enviar" type="radio" value="sim" onclick="document.form1.imagem.disabled=false;" />  Não <input name="enviar" type="radio" value="nao" checked="checked" /></td>
  </tr>
  <tr>
    <td>Imagem:</td>
    <td><input name="imagem" type="file" disabled="true" /></td> 
  </tr>
  <tr>
    <td colspan="2"><div align="center"><input name="enviar" type="submit" value="Enviar"  /></div></td>

  </tr>
</table>

</form>

</body>
</html>

Scripts recomendados

Gerando vários arquivos com PHP usando MPDF

Visualizador de Imagens em PHP-GTK2

Upload de arquivos com barra de progresso muito util

txt2srt

TFtp


  

Comentários

Nenhum comentário foi encontrado.


Contribuir com comentário




Patrocínio

Site hospedado pelo provedor RedeHost.
Linux banner

Destaques

Artigos

Dicas

Tópicos

Top 10 do mês

Scripts