我的图像未从文件夹中删除

时间:2015-03-31 05:42:18

标签: php mysql unlink

我正在尝试从图像文件夹和数据库中删除图像。在我的脚本中它从数据库中删除图像,但它没有从图像文件夹中删除。

到目前为止,这是我的代码:

if ((isset($_POST['imageId'])) && (is_numeric($_POST['imageId'])) ) { // Form submission.
    $imageId = (int)$_POST['imageId'];

    // fetch the selected images to delete
    $query = " SELECT image_id, member_id, image
                         FROM  image_info
                         WHERE image_id = ?";

    $stmt = $mysqli->prepare($query);

    if ($stmt) {
        $stmt->bind_param('i', $imageId);  
        $stmt->execute();    
        $stmt->store_result();
        $numrows = $stmt->num_rows;

        if ($numrows == 1) {
            $stmt->bind_result($image_id, $member_id, $image);

            // define constant for upload folder
            //define('UPLOAD_DIR_2', "images/slider/$member_id");   

            // Fetch all the records:
        while ($stmt->fetch()) {
            if(file_exists("images/slider/$member_id/".$image)) {
                unlink("images/slider/$member_id/". $image); // delete file from folder
                //echo UPLOAD_DIR_2;
            } elseif(is_dir($image)) {
                rmdir($image); // or system("rm -rf " . escapeshellarg($dir)); 
            }
        }               

            // Make the delete query:
            $q = 'DELETE FROM image_info WHERE image_id= ? LIMIT 1'; 
            $stmt = $mysqli->prepare($q);
            $stmt->bind_param('i', $imageId);

            // Execute the query:
            $stmt->execute();

            if ($stmt->affected_rows == 1) {
                $success = "The slideshow image Deleted successfully.";
                echo $successAlert;
            }   
            $stmt->close();
            unset($stmt);
        }
    }
} 

有人可以告诉我这有什么问题吗? 希望有人可以帮助我。

谢谢。

2 个答案:

答案 0 :(得分:0)

请尝试这样,

$path = $_SERVER['DOCUMENT_ROOT']."/images/slider/".$member_id."/".$image;
unlink($path);

更新:

$path = dirname($_SERVER["SCRIPT_FILENAME"])."/images/slider/".$member_id."/".$image;
    unlink($path);

答案 1 :(得分:0)

使用

$path=$_SERVER['DOCUMENT_ROOT'];
exec("chmod -R 777 $path/images/slider/$member_id/$image");
unlink("$path/images/slider/$member_id/$image");