Ok so I am working on a backoffice at work.
This is my first experience on PHP so my code will most likely not be that great.
Here is what I am working on:
The client must be able to upload a image + title + description of a product, it must that show that product on the main page.
After that he must be able to delete or modify that item.
So the upload is working, I managed to show the product, delete it, and modify it.
The only problem is: When I modify with a new image it doesn’t replace the old one in the directory (images take on the id of the sql input, example 0.jpg -> 1.jpg -2.jpg etc.)
So I will show here my upload script and modify :
Upload:
<?php
include("config.php");
$dir = "../images/";
$dir = $dir . basename( $_FILES['image']['name']);
$title = $_POST['title'];
$description = $_POST['desc'];
$position = $_POST['position'];
$image = ($_FILES['image']['name']);
$uploadedfile = $_FILES['image']['tmp_name'];
$uploadedfiletype = $_FILES['image']['type'];
if (!($uploadedfiletype =="image/pjpeg" OR $uploadedfiletype =="image/jpeg" OR $uploadedfiletype =="image/jpg")){
echo "Image must be jpg file";
}else if (move_uploaded_file($_FILES['image']['tmp_name'], $dir)){
$sql="INSERT INTO test (title, description, position) VALUES ('$title','$description','$position')";
if (!mysql_query($sql,$con)){
die('Error: ' . mysql_error());
}
$newname = "../images/" . mysql_insert_id() . ".jpg";
rename ("../images/$image","$newname");
$orig_image = imagecreatefromjpeg("../image/$newname");
$sm_image = imagecreatetruecolor(96,96);
imagecopyresampled($sm_image,$orig_image,0,0,0,0,96,96,imagesx($orig_image),imagesy($orig_image));
imagejpeg($sm_image, $newname, 100);
echo 'Upload good <a href="connected.php">Retour</a>';
}else{
echo "Problem";
}
?>
And here is my modify script (basicly the same thing but with UPDATE for sql):
<?php
include("config.php");
$dir = "../images/";
$dir = $dir . basename( $_FILES['image']['name']);
$title = $_POST['title'];
$description = $_POST['desc'];
$position = $_POST['position'];
$id=$_POST['id'];
$image = ($_FILES['image']['name']);
$uploadedfile = $_FILES['image']['tmp_name'];
$uploadedfiletype = $_FILES['image']['type'];
unlink('../images/$id');
if (!($uploadedfiletype =="image/pjpeg" OR $uploadedfiletype =="image/jpeg" OR $uploadedfiletype =="image/jpg")){
echo "L'image doit être en .jpg ou .jpeg";
}else if (move_uploaded_file($_FILES['image']['tmp_name'], $dir)){
$sql="UPDATE test SET title='$title', description='$description' WHERE id='$id'";
if (!mysql_query($sql)){
die('Error: ' . mysql_error());
}
$newname = "../images/" . mysql_insert_id() . ".jpg";
rename ("../images/$image","$newname");
$orig_image = imagecreatefromjpeg("../image/$newname");
$sm_image = imagecreatetruecolor(96,96);
imagecopyresampled($sm_image,$orig_image,0,0,0,0,96,96,imagesx($orig_image),imagesy($orig_image));
imagejpeg($sm_image, $newname, 100);
echo 'Modif réussi <a href="connected.php">Retour</a>';
}else{
echo "Un probleme est survenue";
}
?>
Thanks in advance for any help !
It seems you got a typo error near
unlink('../images/$id');I mean, PHP doesn’t compile $id within the single quotes.Try doing
unlink("../images/$id");orunlink('../images/'.$id);If it still doesn’t work, You might have a right issue. Try debugging it by doing
var_dump(unlink("../images/$id"))if it returns false, it means deletion failed.If so, try creating a new file, using a sha1 hash for example:
$dir = "../images/".sha1(rand())."-".basename( $_FILES['image']['name']);This will create a new unique name for each uploaded file. It will store the old images, but under different name.
Just call them differently into your database