php 如何在php中取消链接图像

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5713888/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 22:18:19  来源:igfitidea点击:

How to unlink image in php

php

提问by souce code

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

当我回显 $image['image']; 这会给我

http://www.example.com/folder/images/image_name.jpeg
记录已成功删除,但图像仍保留在服务器上。

回答by oezi

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

您必须使用服务器上的路径来删除图像,而不是 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.

你应该删除@before unlink(),在这种情况下你会看到错误消息说“找不到文件”或类似的东西。

回答by Rajazk

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

简单地,如果您folder/images/image_name.jpeg在 unlink 中代替整个 url使用它会正常工作,例如
unlink(" http://www.example.com/folder/images/image_name.jpeg");

should be replaced with

应该替换为

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

回答by Pricope Cosmin

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:

您应该使用相对路径从取消链接的服务器中删除文件。如果将绝对路径保存在数据库中,首先必须查看从哪个文件夹中删除图像。因此,如果您从 www.example.com/folder/delete.php 中的“delete.php”中删除图像,那么您应该执行以下操作:

$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";}
    }

回答by pratik

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.

每当您在删除链接中选择您的代码时。如:<a href=addproduct.php?action=delete&pid=$get_info[pid]>Delete</a>那么您必须使用当前选择项检查条件。

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

回答by antelove

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