1

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);
T Shoats
  • 347
  • 2
  • 4
  • 19
  • What functions give you **permission denied** specifically? Just one or all the file-related functions? – GiamPy Jan 05 '14 at 01:53
  • Does it happen through php execution from a command line or through loading a web page? – Grzegorz Jan 05 '14 at 01:55
  • @GiamPy I've currently tried unlink(), and rmdir dosent really give me the functionality that I need. rmdir gives me an error telling me that it cant remove files because the dir is not empty which makes sense. – T Shoats Jan 05 '14 at 01:57
  • @Grzegorz It happens when I load a web page. – T Shoats Jan 05 '14 at 01:57
  • http://stackoverflow.com/questions/13594898/permission-denied-php-unlink Possible duplicate? – GiamPy Jan 05 '14 at 01:58

3 Answers3

4

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.

Reza Mamun
  • 5,991
  • 1
  • 43
  • 42
0

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

    }
vrajesh
  • 2,935
  • 3
  • 25
  • 40
0

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.

Sablefoste
  • 4,032
  • 3
  • 37
  • 58