0
I need to make, that the user changes the image without changing the source file of the Database, that is, when he closes the BROWSER after having made the exchange, he or any other user entering the Site again, will find the same starting image, because it is stored in the comic book.
And for this I have been researching, but not understanding the reasoning, and I saw that using $_SESSION or even $_COOKIE I could have the desired result, the user exchanges the image for another coming from his own computer, and this image be stored in a SESSION or even as a COOKIE. But I am not able to create the code to accomplish such task by not enter the reasoning of the tutorials I analyzed, which left me with the following doubts:
What would be the target folder of the images?
How to keep the image’s source name when uploading the image?
Because the upload I use (posted below), changes the source name of the files, so there are no duplicates of them, using Rand() for this.
Have friends show me how to build the code with the SESSION or COOKIE, based on the example of the address http://www.efacil.com/?
Page Code edit_logo-upd.php:
<?php
include 'cabecalho.php';
$logo = $_POST['logo'];
$query = mysql_query("SELECT * FROM pagcabecalho");
$res = mysql_fetch_array($query);
?>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252" />
<div align="center" style="margin: 0 0 0 180px; max-width:1000px;">
<?php
$codigo = $_GET['codigo'];
$query = mysql_query("SELECT * FROM pagcabecalho WHERE codigo = '$codigo'");
$res = mysql_fetch_array($query);
$pasta = '../upload/';
if (isset($_POST['logo'])){
$check = @$_POST['apagar'];
foreach($check as $logo){
$delcheck = mysql_query("UPDATE pagcabecalho SET logo='' WHERE codigo = '$codigo'") or die (mysql_error());
unlink($pasta.'/'.$logo);
if ($delcheck >= '1'){
echo 'Imagem deletada com sucesso!';
}else{
echo 'Erro ao deletar imagem, tente novamente!';
}
}
}
?>
<?php include 'upload_logo.php'; ?>
<form action="" method="POST" enctype="multipart/form-data">
<?php
include '../conexao.php';
$seleciona = "SELECT * FROM pagcabecalho";
$queryum = mysql_query($seleciona);
while ($list = mysql_fetch_array($queryum)){
$logo = $list['logo'];
?>
<input size="1" type="hidden" name="codigo" value="<?php echo $codigo;?>" readonly>
<input type="hidden" type="checkbox" name="apagar[]" value="<?php echo $logo;?>" checked readonly>
<?php
}
?>
<label><h4>Selecione uma nova imagem como Logo:</h4></label><br />
<input type="file" name="logo[]" accept="image/*" ><br />
<input type="submit" name="logo" value="Atualizar">
</form>
</div>
Page Code upload_logo.php:
if(isset($_POST['logo'])){
//INFO IMAGEM
$file = $_FILES['logo'];
$numFile = count(array_filter($file['name']));
//PASTA
$folder = '../upload';
//REQUiSITOS
$permite = array('image/jpeg', 'image/png', 'image/gif');
$maxSize = 1024 * 1024 * 5;
//MENSAGEM
$msg = array();
$errorMsg = array(
1 => 'O arquivo no upload é maior que o Limite de finido em upload_maxsize',
2 => 'O arquivo ultrapassa o limite de tamanho em Max_file_size',
3 => 'O upload do arquivo foi feito parcialmente',
4 => 'Não foi feito o upload do arquivo',
);
if($numFile <= 0)
echo 'Selecione uma Imagem!!';
else{
for($i = 0; $i < $numFile; $i++){
$name = $file['name'][$i];
$type = $file['type'][$i];
$size = $file['size'][$i];
$error = $file['error'][$i];
$tmp = $file['tmp_name'][$i];
$extensao = @end(explode('.', $name));
$novoNome = rand().".$extensao";
if($error != 0)
$msg[] = "<b>$name :</b> ".$errorMsg[$error];
else if (!in_array($type, $permite))
$msg[] = "<b>$name :</b> Erro imagem não suportada!";
else if($size > $maxSize)
$msg[] = "<b>$name :</b> Erro imagem ultrapassa o limite de 5MB!";
else{
if(move_uploaded_file($tmp, $folder."/".$novoNome))
$logo = $_FILES['logo'];
$update = mysql_query("UPDATE pagcabecalho SET logo = '$novoNome' WHERE codigo = '$codigo'");
if($update == ''){
echo "<script language='javascript'>
window.alert('Erro ao atualizar Imagem!!!');
</script>";
}else{
echo "<meta http-equiv='refresh' content='0; URL= ../admin/interna.php'>
<script language='javascript'>
window.alert('Imagem atualizada com sucesso!');
</script>";
}}}}}
?>
Already my thanks for the attention to my doubt, and hugs to all.
Thanks João, Thank you for the example, I will take a look very carefully to learn a little about SESSION. And thanks for the tip about MYSQL.
– Murilo Cabral
John, I just tested your example, and it almost worked the way I need it. When changing the image it changes the BD by removing the name of the file referring to the logo, and I have to be updating the page 'edit_logo-upd.php' for the new image to appear, and on the main page of the Site it neither updating appears, nor updated nor the previous one, 'cause he takes it out of the comic book. I’m leaving the address (http://www.efacil.com/) so you. can see what’s happening, thanks? But it’s given me a light, Brian.
– Murilo Cabral
PS. On the main page also inserted aligns the image in this way:
<img width="150" src="<?=$_SESSION['logo']?>" />
, this correct?– Murilo Cabral
Yes, you do. You put session_start at the top of the main page?
– João Sobral
Hello John, face must have been the sleep or even the lack of attention, and I believe I aids the second hypothesis, because the problem was in the lack of
session_start()
on the other pages. Now it’s working... Access address (http://www.efacil.com/). But I came across two situations. First, The files are stored in the upload folder on the server, and this can cause an over load of space, for images that are not being used, because it would be a simulator. Has to direct to a folder type a C: TEMP/ of the user himself?– Murilo Cabral
To Second doubt is how I can keep the image of the Database when entering the page? I mean, I could work with the two PHP’s, the
<img width="150" src="upload/<?php echo $res['logo']; ?>" />
which brings the BD file, and only when the user sends a new image would be replaced by the<img width="150" src="<?=$_SESSION['logo']?>" />
that picks up the file from$_SESSION
!!! I know I’m abusing, but there’s a way you can give me more light... From now on thanking, because it helped your guidance, BRIDADÃO MESMO.– Murilo Cabral
In case you store the client side image I think it is not possible with PHP, and also not recommended, because you should never trust the user input. In case you use two resources, database and Session, you can use if or ternary:
<?=(isset($_SESSION['logo']))?$_SESSION['logo']:$res['logo']?>
This code checks if $_SESSION['logo'] exists, if it exists it calls SESSION, if it doesn’t call $res.– João Sobral