繁体   English   中英

从文件夹删除文件

[英]Deleting file from folder

我不太确定问题出在哪里。 但是代码不会取消链接文件:(

 <?php include_once("sessions.php");
require_once("connect.php");
if(isset($_POST['delete'])){

$album_id = $_SESSION['album_id'];

$checkbox = $_POST['photo_checkbox'];
$count = count($checkbox);

for($i = 0; $i < $count; $i++) {
    $id = (int) $checkbox[$i]; // Parse your value to integer

    if ($id > 0) { // and check if it's bigger then 0

        $query = "SELECT * FROM media WHERE id = $id";
        $result = mysqli_query($connection, $query);
            while($row = mysqli_fetch_array($result)){

            $file = $row['path'];

                if(!unlink($file)){
                    $_SESSION["edit_message"] = "<br>Something went wrong while deleting shit ... please try your editing again." .$file;
                    header ("Location: ../fotos.php?album=".$album_id."");
                    exit;
                }

            }
        $query = "DELETE FROM media WHERE id = $id";
        $result = mysqli_query($connection, $query);
    }
}   

    if($result){
        $_SESSION["edit_message"] = "<br>Successfully deleted !";
        header ("Location: ../fotos.php?album=".$album_id."");
        exit;}
}

?>

如果我去掉unlink循环部分,然后直接从数据库中删除它就可以了。 我想念什么? 可能是权限阻碍了代码的执行?

编辑:现在将文件的权限更改为0777。 因此它应该确实起作用。但是似乎仍然没有。 :/我现在没有想法。 也许循环工作不正常?

感谢您的帮助

干杯

克里斯

$file2 = chmod($file, 0777);

if(!unlink($file2)){

$ file2正在获取chmod的返回值,这是一个布尔值。 然后,您尝试取消链接true / false值。 也许您打算取消链接($ file)?

编辑以反映您的更改:

如果$ file不是完全限定的路径名​​,则$ file将相对于脚本运行所在位置的当前工作目录。 确保$ file是完整路径名。

对文件的写权限还不够,您需要对目录本身具有写权限才能删除其中的文件。

您应该首先检查文件是否存在,然后再检查对目录(不是文件)的正确权限。

if(file_exists($file) && is_writeable(dirname($file))){
unlink($file);
}else{
//invalid path or permission problems
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM