1

I have row that contains (id, name, pic), I can delete this row from the database, but cannot delete the image from the file server.

Below code should delete the image file:

if(isset($_GET["delete"])){

$pi=$_GET["delete"];
$qry="delete from item where id=".$_GET["delete"];
$de = mysqli_query($conn,$qry);

$filetmp = $_FILES["pic"]["tmp_name"];
// $filename = $_FILES["pic"]["name"];
$qr ="SELECT id FROM item where id='$pi'";
$res = mysqli_query($conn,$qr);
while($row = mysqli_fetch_array($res)){
$id = $row["id"];
}
$path = "uploads/$id.jpg";
//move_uploaded_file($filetmp,$path);
$fpath = "images_upload/Uitem/$path";
unlink($fpath); // delete file

}
Blueblazer172
  • 588
  • 2
  • 15
  • 44
Abdulelah
  • 53
  • 1
  • 1
  • 5
  • 1
    Not directly related to your problem, but worth a read: http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php – Shira Dec 09 '16 at 10:05
  • 1
    Enable displaying of PHP errors and try again: http://stackoverflow.com/questions/1053424/how-do-i-get-php-errors-to-display – Shira Dec 09 '16 at 10:06
  • your mysqli statements are wrong... – Blueblazer172 Dec 09 '16 at 10:07
  • Ciuld you please check, whether this is possibly permission related (e.g. your webserver user is allowed to delete the file)? – randomnickname Dec 09 '16 at 10:10
  • Possible duplicate of [delete image from folder PHP](http://stackoverflow.com/questions/15005899/delete-image-from-folder-php) – Masivuye Cokile Dec 09 '16 at 10:36

1 Answers1

0

you'll have to use the path on your server to delete the image, not the image url. Like

unlink('/var/www/test/'.$fpath);
Alok Bichhwe
  • 172
  • 2
  • 8