PHP permission Denied using unlink and rmdir

T Shoats picture T Shoats · Jan 5, 2014 · Viewed 17.7k times · Source

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

Answer

Reza Mamun picture Reza Mamun · Jan 5, 2014

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.