1

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.

2 Answers 2

2

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

0
1

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.