删除文件夹中的特定图像?

时间:2014-06-16 11:46:32

标签: php mysql

如何从文件夹中删除图像?我的代码删除了数据库中的图像路径,但没有在文件夹中删除,我也希望删除文件夹中的图像。

我的代码在这里

<?php
$id=$_POST['id'];
include_once('db.php');
$objDbCon = new db_connect();
$strSQL="DELETE FROM clients WHERE id='$id'";
$objQueryHome = $objDbCon->Query($strSQL);

if($objQueryHome){
    echo "Record has been deleted Succesfully";
}
else{
    echo "Error";
}
?>

我的图片文件夹路径为../uploaded/

2 个答案:

答案 0 :(得分:2)

您可以使用unlink() php功能

<?php

if($objQueryHome){
    unlink('image_path/image_name.jpg');
    echo "Record has been deleted Succesfully";
}
else{
    echo "Error";
}
?>

答案 1 :(得分:0)

尝试使用以下代码 -

<?php
    $id=$_POST['id'];
    include_once('db.php');

$objDbCon = new db_connect();
// first get the records --
$strSQLrecord ="Select * FROM clients WHERE id='$id'";
$result  = $objDbCon->Query($strSQLrecord);

while($newrow  = $result){
    # print_r($newrow) ; // just to get record details
    // get image name, may be something like below
    $imag = $newrow['image'] ; // where image is fields name in table where image-name is stored
    $fullpath = 'uploaded/'.$img ;
}

$strSQL="DELETE FROM clients WHERE id='$id'";
$objQueryHome = $objDbCon->Query($strSQL);

if($objQueryHome){
    unlink($fullpath) ; // unlink will be returning bool values if it was able to delete file otherwise check with the files path
    echo "Record has been deleted Succesfully";
}
else{
    echo "Error";
}
?>