使用复选框成功从数据库中删除多个图像,但不会取消文件与文件夹的链接

Using checkboxes to delete multiple images from db successfully but doesn't unlink files from folder

我对 PHP 中的 unlink() 函数有疑问。我使用了复选框来删除图像或上传的视频文件。

使用 unlink().

从数据库中成功删除了多个图像或视频,但未从本地文件夹中删除

没有错误,但是当我 select 所有复选框并按删除时,数据库中的所有文件都被删除,但只有第一个文件未链接。

这是我的代码:

<?php // for delete checked video php script

include('config.php');

if(isset($_POST['delete'])) {
    $checkedCandidates = 0;
    $id = implode(",", $_POST['deletecb']);

    $checkedCandidates = count($id);

    if ($checkedCandidates < 1) {
        echo "<div id=\"errormsg\"> You need to check at least one video for delete. </div>";
        echo "<script>setTimeout(\"location.href = 'video_upload.php';\",3000);</script>";
    } else {
        $result=mysqli_query($connection,"SELECT * FROM video_gallery where id_vid IN($id) and users_name='$login_session'");

        while ($row=mysqli_fetch_assoc($result)) {
            $fname=$row[FILE_NAME];

            $Path="data/58f60f2e09f07_jay/videos/$fname";
            if (file_exists($Path)){
                if (unlink($Path)) {
                    echo "success";
                } else {
                    echo "fail";
                }
            } else {
                echo "file does not exist";
            }
        }
        $query1=mysqli_query($connection,"delete from video_gallery where id_vid IN($id) and users_name='$login_session'");
        if($query1) {
            echo "<div id=\"successmsg\"> delete successfully </div>";
        } else {
            echo "<div id=\"errormsg\"> failed operation!!</div>";
        }
    }
}
?>

尝试print_r($row); 并检查文件名的键名 在访问数组项时更改此行

$fname=$row[FILE_NAME]; 

$fname=$row['the actual name of the field']; 

$Path="data/58f60f2e09f07_jay/videos/$fname";替换为$Path="data/58f60f2e09f07_jay/videos/".$fname;

并将$row[FILE_NAME]替换为$row['FILE_NAME']