如何使用 PHP 和 MySQL 删除图像?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2655546/
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 07:13:57  来源:igfitidea点击:

How to delete an image using PHP & MySQL?

phpmysql

提问by IMAGE

I was wondering if some one can give me an example on how to delete an image using PHP & MySQL?

我想知道是否有人可以给我一个关于如何使用 PHP 和 MySQL 删除图像的示例?

The image is stored in a folder name thumbs and another named images and the image name is stored in a mysql database.

图像存储在一个名为thumbs 的文件夹中,另一个名为images 的文件夹中,图像名存储在mysql 数据库中。

回答by Joshua Enfield

Delete the file:

删除文件:

unlink("thumbs/imagename");

unlink("images/imagename");

Remove from database

从数据库中删除

$sql="DELETE FROM tablename WHERE name='imagename'"

$result=mysql_query($sql);

Assuming nameis the the name of the field in the database holding the image name, and imagenameis the image's name.

假设name是数据库中保存图像名称的字段的名称,imagename是图像的名称。

All together in code:

全部在代码中:

$imgName='sample.jpg';
$dbFieldName='name';
$dbTableName='imageTable';
unlink("thumbs/$imgName");
unlink("images/$imgName");
$sql="DELETE FROM $dbTableName WHERE $dbFieldName='$imgName'";
mysql_query($sql);

回答by Hanseh

try this code :

试试这个代码:

$img_dir = 'image_directory_name/';
$img_thmb = 'thumbnail_directory_name/';// if you had thumbnails

$image_name = $row['image_name'];//assume that this is the image_name field from your database

//unlink function return bool so you can use it as conditon
if(unlink($img_dir.$image_name) && unlink($img_thmb.$image_name)){
    //assume that variable $image_id is queried from the database where your image record your about to delete is...
    $sql = "DELETE FROM table WHERE image_id = '".$image_id."'";
    $qry = mysql_query($sql);
}else{
   echo 'ERROR: unable to delete image file!';
}

回答by Serge

Are you looking for actual code or just the idea behind it?

您是在寻找实际的代码还是只是在寻找其背后的想法?

You'll need to query the db to find out the name of the file being deleted and then simply use unlink to delete the file in question.

您需要查询数据库以找出要删除的文件的名称,然后只需使用 unlink 删除有问题的文件。

so here's some quick code to get you started

所以这里有一些快速代码可以帮助您入门

<?php
$thumb_dir = "path/to/thumbs/";
$img_dir = "path/to/images/";

/* query your db to get the desired image
   I'm guessing you're using a form to delete the image?
   if so use something like $image = $_POST['your_variable'] to get the image 
   and query your db */

// once you confirm that the file exists in the db check to see if the image 
// is actually on the server

if(file_exists($thumb_dir . $image . '.jpg')){
    if (unlink($thumb_dir . $image . '.jpg') && unlink($img_dir . $image . '.jpg'))
        //it's better to use the ID rather than the name of the file to delete it from db
        mysql_query("DELETE FROM table WHERE name='".$image."'") or die(mysql_error());
}
?>

回答by chetan borse

if(!empty($_GET['pid']) && $_GET['act']=="del")
{
    $_sql = "SELECT * FROM mservices WHERE pro_id=".$_GET['pid'];

    $rs = $_CONN->Execute($_sql);
    if ($rs->EOF) {
        $_MSG[] = "";
        $error = 1;
    }

    if ($rs)
        $rs->close();
    if (!$error) {

    $_Image_to_delete = "select pro_img from mservices where pro_id=".$_GET['pid'];
    $trial=$_CONN->Execute($_Image_to_delete);
    $img = trim(substr($trial,7));
    unlink($_DIR['inc']['product_image'].$img);


      $_sql = "delete from mservices where pro_id=".$_GET['pid'];
        $_CONN->Execute($_sql);
        header("Location: ".$_DIR['site']['adminurl']."mservices".$atend."suc".$_DELIM."3".$baratend);
        exit();
    }
}

回答by K.Prasad

$_sql = "SELECT * FROM mservices WHERE pro_id=".$_GET['pid'];

$rs = $_CONN->Execute($_sql);
if ($rs->EOF) {
    $_MSG[] = "";
    $error = 1;
}

if ($rs)
    $rs->close();
if (!$error) {

$_Image_to_delete = "select pro_img from mservices where pro_id=".$_GET['pid'];
$trial=$_CONN->Execute($_Image_to_delete);
$img = trim(substr($trial,7));
unlink($_DIR['inc']['product_image'].$img);


  $_sql = "delete from mservices where pro_id=".$_GET['pid'];
    $_CONN->Execute($_sql);
    header("Location: ".