Upload de imagens
Publicado por adrianosalles (última atualização em 28/09/2009)
[ Hits: 7.359 ]
Script para fazer o upload de imagens com validações.
<!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>
Listando arquivos do diretório
Classe em php para listar subdiretórios e arquivos em html
Nenhum comentário foi encontrado.
Cirurgia para acelerar o openSUSE em HD externo via USB
Void Server como Domain Control
Modo Simples de Baixar e Usar o bash-completion
Monitorando o Preço do Bitcoin ou sua Cripto Favorita em Tempo Real com um Widget Flutuante
Jogar games da Battle.net no Linux com Faugus Launcher
Como fazer a Instalação de aplicativos para acesso remoto ao Linux
Como fazer a instalação do Samba
Como fazer a conversão binária e aplicar as restrições no Linux
Duas Pasta Pessoal Aparecendo no Ubuntu 24.04.3 LTS (19)
Formas seguras de instalar Debian Sid (13)
Malware encontrado em extensões do Firefox. (0)
Fiz uma pergunta no fórum mas não consigo localizar [RESOLVIDO] (21)









