Upload de imagens
Publicado por adrianosalles (última atualização em 28/09/2009)
[ Hits: 7.379 ]
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>
Classe em php para listar subdiretórios e arquivos em html (vs certa)
Mural de recados com arquivo de log
Upload de arquivos com barra de progresso muito util
Script para fazer upload de arquivos
Nenhum comentário foi encontrado.
LazyDocker – Interface de Usuário em Tempo Real para o Docker
Instalando COSMIC no Linux Mint
Turbinando o Linux Mint: o poder das Nemo Actions
Inteligência Artificial no desenvolvimento de software: quando começar a usar?
[Resolvido] Algo deu errado ao abrir seu perfil
Usando o VNSTAT para medir o seu consumo de internet
Habilitando clipboard manager no ambiente COSMIC
Quando vocês pararam de testar distros? (9)
Quais licenças open source têm valor jurídico? (7)
Não estou conseguindo fazer funcionar meu Postfix na versão 2.4 no Deb... (1)









