简体   繁体   English

从文件夹删除文件

[英]Deleting file from folder

I'm not quite sure where the problem lies. 我不太确定问题出在哪里。 But the code won't unlink the file :( 但是代码不会取消链接文件:(

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

?>

If I take out the unlink loop part and just go straight to the deleting from the db it works fine. 如果我去掉unlink循环部分,然后直接从数据库中删除它就可以了。 What am I missing? 我想念什么? Might it be the permissions that are hindering the code from executing ? 可能是权限阻碍了代码的执行?

EDIT : Changed the permissions of the file to 0777 now. 编辑:现在将文件的权限更改为0777。 So it should really work ... But still doesn't seem to. 因此它应该确实起作用。但是似乎仍然没有。 ! :/ I have no ideas now. :/我现在没有想法。 Maybe the loop isn't working properly ? 也许循环工作不正常?

Thanx for your help 感谢您的帮助

Cheers 干杯

Chris 克里斯

$file2 = chmod($file, 0777);

if(!unlink($file2)){

$file2 is getting the return value of chmod, which is a bool. $ file2正在获取chmod的返回值,这是一个布尔值。 You're then trying to unlink a true/false value. 然后,您尝试取消链接true / false值。 Perhaps you meant to unlink($file) ? 也许您打算取消链接($ file)?

Edit to reflect your changes: 编辑以反映您的更改:

If $file is not a fully qualified path name $file will be relative to the current working directory of where ever the script is running from. 如果$ file不是完全限定的路径名​​,则$ file将相对于脚本运行所在位置的当前工作目录。 Ensure $file is a full path name. 确保$ file是完整路径名。

Write permissions on the file are not sufficient you need write permissions on the directory itself to be able to delete a file within it. 对文件的写权限还不够,您需要对目录本身具有写权限才能删除其中的文件。

You should first check the file exists, you should then check that you have the correct permissions on the directory NOT the 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