Marc Rasmussen
Marc Rasmussen

Reputation: 20555

PHP remove dir cannot remove because of permission

I am trying to delete a folder using PHP however the folder is chmod -R 777 meaning that when PHP attempts to remove the folder it gets permission denied.

This is my delete function:

    private function delTree($dir)
{
    $files = glob( $dir . '*', GLOB_MARK );
    foreach( $files as $file ){
        if( substr( $file, -1 ) == '/' )
            delTree( $file );
        else
            unlink( $file );
    }
    rmdir( $dir );
}

is it possible to work around sudo ? or add sudo permissions to the PHP function.

Upvotes: 1

Views: 170

Answers (2)

xate
xate

Reputation: 6379

Make sure you don't have the directory still open after using opendir(). Especially when writing recursive functions for deleting directories, make sure you have closedir() BEFORE rmdir(). However you don't need sudo when you set permissions 777

Upvotes: 2

René Höhle
René Höhle

Reputation: 27295

Perhaps you have opened the folder with opendir() or fopen() then you're accessing the folder and you can't delete them when you have opened it or if you are insight the folder.

When you set the permission 777 its enough.

Upvotes: 1

Related Questions