How to disable image in php

I upload the image to the server and save the path in the database. Now I want to delete this entry, as well as the image with this entry my code

  $ id = $ _ GET ['id'];
 $ select = mysql_query ("select image from table_name where question_id = '$ id'");
 $ image = mysql_fetch_array ($ select);
 @unlink ($ image ['image']);
 $ result = mysql_query ("delete from table_name where question_id = '$ id'"); 

when I echo $ image ['image']; it will give me

  http://www.example.com/folder/images/image_name.jpeg 
The record is deleted successfully, but the image remains on the server.
+7
source share
5 answers

you will need to use the path on your server to remove the image, not the URL.

unlink('/var/www/test/folder/images/image_name.jpeg'); // correct 

you must delete @ before unlink() , in which case you would see a "file not found" error message or something like that.

+34
source

Just if you use folder/images/image_name.jpeg instead of the whole url inside unlink, it will work fine for example
unlink (" http://www.example.com/folder/images/image_name.jpeg ");

should be replaced by

 unlink("folder/images/image_name.jpeg"); 
+4
source

whenever you select your code in the delete link. like: <a href=addproduct.php?action=delete&pid=$get_info[pid]>Delete</a> then you need to check this condition using the cuurent select element.

 if(isset($_GET['action']) && $_GET['action']=='delete' && isset($_GET['pid'])) { $query1=("select * from tablename where id='".$_GET['id']."'"); $result1=mysql_query($query1); while($data=mysql_fetch_array($result1)) { $delete=$data['file']; unlink("../upload/$delete"); } $query=("delete from tablename where id='".$_GET['id']."'"); $result=mysql_query($query) or die("not inserted". mysql_error()); if($result==TRUE) { $_SESSION['msg']="product successfully deleted"; header("Location:addproduct.php"); exit; } else { $_SESSION['msg']="error in deleting product"; header("Location:addproduct.php"); exit; } } 
+1
source

you must use the relative path to delete the file from the server with the shutdown. If you save the absolute path in your database, you should first see which folder you are deleting the image from. so if you delete the image from "delete.php", which is located at www.example.com/folder/delete.php, you should do something like this:

 $db_path = "http://www.example.com/folder/images/upArrow.png"; $len = strlen("http://www.example.com/folder/"); $new_path = substr($db_path, $len, strlen($db_path)-$len); echo " -> ".$new_path; if(isset($_POST['Submit'])){ $return = unlink($new_path); if($return){echo "Succes";}else{echo "Fail";} } 
0
source
 //http://www.example.com/folder/images/image_name.jpeg define("BASE_URL", DIRECTORY_SEPARATOR . "folder" . DIRECTORY_SEPARATOR); define("ROOT_PATH", $_SERVER['DOCUMENT_ROOT'] . BASE_URL); $folder_upload = "images/"; $image_delete = ROOT_PATH . $folder_upload . pathinfo($image['image'], PATHINFO_BASENAME); if (!empty($image['image'])) { /* Delete */ if (unlink($image_delete)) { echo "<b>{$image_delete}</b> has been deleted"; } else { echo "<b>{$image_delete}</b> error deleting "; } } else { echo "File image not exist"; } // http://localhost/folder/images/image_name.jpeg 
0
source

All Articles