PHP permission Denied using unlink and rmdir

二次信任 提交于 2020-01-05 11:47:05

问题


i've been trying to figure out why my Php code is giving me a annoying error. I've tried countless functions from previous post but the error its been giving is "Permission Denied". From my understanding either i have to have special privledges to delete files, etc.. I've tried multiple solutions but I'm still getting this error. If anyone can point me in the right direction, that'll be great. Ive post a snippet of my code below.. Thanksss

      $first_sub = "my_dir";        
        if(is_dir($first_sub)){
            $read_sub1 = opendir($first_sub);
            while(false !== ($files = readdir($read_sub1))){
                if($files!="." && $files!=".."){
                    unlink($first_sub ."/". $files);
                }
            }
            closedir($read_sub1);

回答1:


You should set proper permission to your server directories: Visit: http://bd1.php.net/chmod

<?php
// Read and write for owner, nothing for everybody else
chmod($first_sub ."/". $files, 0600);

// Read and write for owner, read for everybody else
chmod($first_sub ."/". $files, 0644);

// Everything for owner, read and execute for others
chmod($first_sub ."/". $files, 0755);

// Everything for owner, read and execute for owner's group
chmod($first_sub ."/". $files, 0750);
?>

just before unlink you can call this function.




回答2:


I got that an error from unlink permission denied. But I fix it. The error displays like this unlink(../foldername/) Permission denied.

My wrong code is like this:

$image = select_table('webpage', 'wp_name', '$id');
$update = "UPDATE webpage SET wp_image = NULL, wp_modifiedby = '{$position}', wp_datemodified = '{$date_now}' WHERE wp_name = '{$id}'";

    if ( unlink('../webpage/'.$image_dir) && $qry_update = mysqli_query($connection, $update) ) {
        // success
    } else {
        // failed
    }

now i fix it my correct code is like this:

$image = select_table('webpage', 'wp_name', $id);

    $update = "UPDATE webpage SET wp_image = NULL, wp_modifiedby = '{$position}', wp_datemodified = '{$date_now}' WHERE wp_name = '{$id}'";

    if ( unlink('../webpage/'.$image['wp_image']) && $qry_update = mysqli_query($connection, $update) ) {
        // success

    } else {
        // failed

    }



回答3:


For those who land on this page, it may be as simple as not setting $files to an existing file.

It is unfortunate, but I found that the message: Warning: move_uploaded_file(): Unable to move can also mean file not found.

Not likely the cause of this OP's problem, but certainly worth verifying the file represented by the variable you pass actually exists in the directory.



来源:https://stackoverflow.com/questions/20929101/php-permission-denied-using-unlink-and-rmdir

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!