duandu8707 2015-03-31 05:42
浏览 18
已采纳

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

I am trying to remove images from image folder and database. In my script it delete image from database but it is not removing from the image folder.

This is my CODE so far:

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

Can anybody tell me what is wrong with this? Hope somebody may help me out.

Thank you.

  • 写回答

2条回答 默认 最新

  • douniao8687 2015-03-31 05:45
    关注

    please try like this,

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

    UPDATED:

    $path = dirname($_SERVER["SCRIPT_FILENAME"])."/images/slider/".$member_id."/".$image;
        unlink($path);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 unity第一人称射击小游戏,有demo,在原脚本的基础上进行修改以达到要求
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)