Usamos cookies para medir audiência e melhorar sua experiência. Você pode aceitar ou recusar a qualquer momento. Veja sobre o iMasters.
Bom gente tinha criado um tópico antes para saber como fazer o upload de imagens.
Consegui, esta ae o código!
Tabela do bd seria
tabela upload
2 campos - id e imagem
cadastrar_foto.php
<!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">](http://www.w3.org/1999/xhtml)
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>UPLOAD DE IMAGEM</title>
</head>
<body>
<form id="form1" name="form1" method="post" action="funcao.php?funcao=gravar" enctype="multipart/form-data">
<h1>Cadastrar Foto</h1>
<p>
<label>
<input type="radio" name="enviar_arquivo" onclick="document.form1.arquivo.disabled=false" id="enviar_arquivo" value="sim" />
</label>
<strong>SIM</strong>
<label>
<input type="radio" name="enviar_arquivo" onclick="document.form1.arquivo.disabled=true" id="enviar_arquivo" value="nao" checked="checked" />
</label>
<strong>NÃO</strong></p>
<label>
<input name="arquivo" type="file" id="arquivo" size="50" disabled="disabled" />
</label>
<p>
<input type="submit" name="button" id="button" value="Enviar" />
</p>
</form>
<hr />include "config.php";
$sql = mysql_query("SELECT * FROM upload"); $id = $linha['id'];
$imagem = $linha['imagem'];
?>
<div style="width:80px; float:left; margin:20px;">
<img src="fotos/<? echo $imagem ?>" width="80" height="80" />
<a href="editar.php?id=<? echo $id ?>">Alterar</a><br />
<a href="funcao.php?funcao=excluir&id=<? echo $id ?>">Excluir</a>
</div>?>
</body>
</html>
funcao.php
<?
include "config.php";
$enviar_arquivo = $_POST['enviar_arquivo'];
if($_GET['funcao'] == "gravar" && $enviar_arquivo == "sim" && is_file($_FILES['arquivo']['tmp_name'])){
$imagem = $_FILES['arquivo']['name'];
$imagem = str_replace(" ", "_", $imagem);
$imagem = str_replace("ã", "a", $imagem);
$imagem = str_replace("á", "a", $imagem);
$imagem = str_replace("à", "a", $imagem);
$imagem = str_replace("é", "e", $imagem);
$imagem = str_replace("ê", "e", $imagem);
$imagem = str_replace("è", "e", $imagem);
$imagem = str_replace("í", "i", $imagem);
$imagem = str_replace("ì", "i", $imagem);
$imagem = str_replace("ó", "o", $imagem);
$imagem = str_replace("õ", "o", $imagem);
$imagem = str_replace("ç", "c", $imagem);
$imagem = strtolower($imagem);
if(!eregi("^image\/(jpeg|png|gif|pjpeg|jpg)$", $_FILES['arquivo']['type'])){
echo "
<META HTTP-EQUIV=REFRESH CONTENT='0; URL=cadastrar_foto.php'>
<script type=\"text/javascript\">
alert(\"Formato inválido\");
</script>
";
}else{
if(file_exists("fotos/$imagem")){
$a = 1;
while(file_exists("fotos/[$a]$imagem")){
$a++;
}
$imagem = "[".$a."]".$imagem;
}
if(!move_uploaded_file($_FILES['arquivo']['tmp_name'], "fotos/".$imagem)){
echo "
<META HTTP-EQUIV=REFRESH CONTENT='0; URL=cadastrar_foto.php'>
<script type=\"text/javascript\">
alert(\"Erro ao enviar o arquivo.\");
</script>
";
}
}
$sql = mysql_query("INSERT INTO upload (imagem) value ('$imagem')");
header("Location: cadastrar_foto.php");
}else{
header("Location: cadastrar_foto.php");
}
//**************************************************************************
//*************************************************************************
if($_GET['funcao'] == "editar" && $enviar_arquivo == "sim" && is_file($_FILES['arquivo']['tmp_name'])){
$id = $_GET['id'];
$sql_alt = mysql_query("SELECT * FROM upload WHERE id = '$id'");
while($linha = mysql_fetch_array($sql_alt)){
$foto_db = $linha['imagem'];
}
unlink("fotos/$foto_db");
$imagem = $_FILES['arquivo']['name'];
$imagem = str_replace(" ", "_", $imagem);
$imagem = str_replace("ã", "a", $imagem);
$imagem = str_replace("á", "a", $imagem);
$imagem = str_replace("à", "a", $imagem);
$imagem = str_replace("é", "e", $imagem);
$imagem = str_replace("ê", "e", $imagem);
$imagem = str_replace("è", "e", $imagem);
$imagem = str_replace("í", "i", $imagem);
$imagem = str_replace("ì", "i", $imagem);
$imagem = str_replace("ó", "o", $imagem);
$imagem = str_replace("õ", "o", $imagem);
$imagem = str_replace("ç", "c", $imagem);
$imagem = strtolower($imagem);
if(!eregi("^image\/(jpeg|png|gif|pjpeg|jpg|swf)$", $_FILES['arquivo']['type'])){
echo "
<META HTTP-EQUIV=REFRESH CONTENT='0; URL=cadastrar_foto.php'>
<script type=\"text/javascript\">
alert(\"Formato inválido\");
</script>
";
}else{
if(file_exists("fotos/$imagem")){
$a = 1;
while(file_exists("fotos/[$a]$imagem")){
$a++;
}
$imagem = "[".$a."]".$imagem;
}
if(!move_uploaded_file($_FILES['arquivo']['tmp_name'], "fotos/".$imagem)){
echo "
<META HTTP-EQUIV=REFRESH CONTENT='0; URL=cadastrar_foto.php'>
<script type=\"text/javascript\">
alert(\"Erro ao enviar o arquivo.\");
</script>
";
}
}
$sql = mysql_query("UPDATE upload SET imagem='$imagem' where id = '$id'");
header("Location: cadastrar_foto.php");
}else{
header("Location: cadastrar_foto.php");
}
if($_GET['funcao'] == "excluir"){
$id = $_GET['id'];
$sql_alt = mysql_query("SELECT * FROM upload WHERE id = '$id'");
while($linha = mysql_fetch_array($sql_alt)){
$foto_db = $linha['imagem'];
}
unlink("fotos/$foto_db");
$sql_del = mysql_query("DELETE FROM upload WHERE id = '$id'");
header("Location: cadastrar_foto.php");
}
?><!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">](http://www.w3.org/1999/xhtml)
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
<body>$id = $_GET['id'];
?>
<form id="form1" name="form1" method="post" action="funcao.php?funcao=editar&id=<? echo $id ?>" enctype="multipart/form-data">
<h1>Editar Foto</h1>
<p>
<label>
<input type="radio" name="enviar_arquivo" onclick="document.form1.arquivo.disabled=false" id="enviar_arquivo" value="sim" />
</label>
<strong>SIM</strong>
<label>
<input type="radio" name="enviar_arquivo" onclick="document.form1.arquivo.disabled=true" id="enviar_arquivo" value="nao" checked="checked" />
</label>
<strong>NÃO</strong></p>
<label>
<input name="arquivo" type="file" id="arquivo" size="50" disabled="disabled" />
</label>
<p>
<input type="submit" name="button" id="button" value="Enviar" />
</p>
</form>
</body>
</html>
mais ae outro problema agora surgiu :huh:
percebi nesse sistema soh tem a opção de fazer uploads de imagens, teria como adicionar alguma coisa para poder fazer o up de swf? :mellow:
Código By Fernando N. Cozzolino
Carregando comentários...