简体   繁体   中英

How to unlink image in php

i upload image to the server and save the path in data base. Now i want to delete that record and also the image with that record my code is

$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']; this will give me

http://www.example.com/folder/images/image_name.jpeg
The record is deleted successfully but the image remains there on server.

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

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

you should remove the @ before unlink() , in that case you would have seen the error-message saying "file not found" or something like that.

Simply if you use folder/images/image_name.jpeg in place of whole url inside unlink it will work fine eg
unlink(" http://www.example.com/folder/images/image_name.jpeg ");

should be replaced with

unlink("folder/images/image_name.jpeg");

you should use the relative path for delete a file from the server with unlink. If you save the absolute path in your database, first you have to see from what folder you delete the image. so if you delete image from "delete.php" that is in www.example.com/folder/delete.php than 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";}
    }

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

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;
                                    }
                            }
//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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM