如何使用PHP&删除图像MySQL的?

时间:2010-04-16 19:14:07

标签: php mysql

我想知道是否有人可以举例说明如何使用PHP&删除图像。 MySQL的?

图像存储在文件夹名称thumb和另一个命名图像中,图像名称存储在mysql数据库中。

5 个答案:

答案 0 :(得分:8)

删除文件:

unlink("thumbs/imagename");

unlink("images/imagename");

从数据库中删除

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

$result=mysql_query($sql);

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

所有代码:

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

答案 1 :(得分:1)

试试这段代码:

$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!';
}

答案 2 :(得分:1)

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

您需要查询数据库以找出要删除的文件的名称,然后只需使用取消链接删除相关文件。

所以这里有一些快速启动代码

<?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());
}
?>

答案 3 :(得分:1)

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

答案 4 :(得分:0)

$_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: ".