2

I have been working on a project that involves a step, during which the script needs to automatically remove a certain directory in Linux ( and all its contents ).

I am currently using the following code to do that:

# Perform a recursive removal of the obsolete folder
$dir_to_erase = $_SESSION['path'];
function removeDirectory($dir_to_erase) {
    $files = glob($dir_to_erase . '/*');
    foreach ($files as $file) {
        is_dir($file) ? removeDirectory($file) : unlink($file);
    }
    rmdir($dir_to_erase);
    return; 
}

Where $_SESSION['path'] is the folder to erase. Been working like a charm, but I recently had to add a .htaccess file to the folder, and I noticed the script stopped working correctly ( it keeps removing the rest of the files fine, but not the .htaccess files ).

Can anyone point me as to what I should add to the code include the hidden dot files in the removal process?

albert
  • 5,966
  • 3
  • 13
  • 29
Peter
  • 41
  • 6
  • /* does not match files starting with a ".". try it on the bash shell. I assume changing /* in your script to /. should actually do the job on Linux systems. I never used glob though. – Mathijs Segers Mar 17 '17 at 08:34

3 Answers3

1

You can slightly modify your function to remove hidden files also:

function removeDirectory($dir) 
{
    if (is_dir($dir)) { 
        $objects = scandir($dir); 
        foreach ($objects as $object) { 
            if ($object != "." && $object != "..") { 
                if (is_dir($dir."/".$object))
                    removeDirectory($dir."/".$object);
                else
                    unlink($dir."/".$object); 
            } 
        }
        rmdir($dir); 
    } 
}
PHPLego
  • 269
  • 2
  • 6
  • Thank you for the response. When I modify it that way, I get a truckload of errors and nothing gets erased at all: – Peter Mar 17 '17 at 08:14
  • Warning: glob(): Pattern exceeds the maximum allowed length of 4096 characters in /home/chalouco/public_html/hihi/main/cleanup.php on line 20 Warning: glob(): Pattern exceeds the maximum allowed length of 4096 characters in /home/chalouco/public_html/hihi/main/cleanup.php on line 21 Warning: array_merge(): Argument #1 is not an array in /home/chalouco/public_html/hihi/main/cleanup.php on line 22 Warning: Invalid argument supplied for foreach() in /home/chalouco/public_html/hihi/main/cleanup.php on line 22 – Peter Mar 17 '17 at 08:15
  • hmm... looks like glob function gets too long string as argument. It means some of files/folders you delete have a path longer than 4096 characters – PHPLego Mar 17 '17 at 08:20
  • The function works just fine without the recent addition - none of the folders and files have such long paths. Only started happening after I added the new code. – Peter Mar 17 '17 at 08:23
  • I happens because now function "dives into" hidden folders also – PHPLego Mar 17 '17 at 08:25
  • How can I prevent that from happening? Meaning only include hidden files like .htaccess, and exclude hidden folders completely? Also, there are no hidden folders at all. – Peter Mar 17 '17 at 08:26
  • The problem was in special folders '.' and '..'. We need to exclude them. I have changed the function – PHPLego Mar 17 '17 at 08:31
  • Still getting errors: Warning: glob(): Pattern exceeds the maximum allowed length of 4096 characters in /home/chalouco/public_html/hihi/main/cleanup.php on line 20 Warning: glob(): Pattern exceeds the maximum allowed length of 4096 characters in /home/chalouco/public_html/hihi/main/cleanup.php on line 21 Warning: array_diff(): Argument #1 is not an array in /home/chalouco/public_html/hihi/main/cleanup.php on line 22 Warning: array_merge(): Argument #1 is not an array in /home/chalouco/public_html/hihi/main/cleanup.php on line 23 – Peter Mar 17 '17 at 08:39
  • Just to clarify - the dir to erase is passed in the format "data/d2700c09bed1aeeab1bcc56cfe8a5b51" , where the hash is a random string. Would it work if I defined this as an absolute path instead? – Peter Mar 17 '17 at 08:43
  • I have changed the code again. scandir is easier for this purpose. Now it should work fine. Finally :) – PHPLego Mar 17 '17 at 08:54
  • Htaccess gets removed fine now, but it does not erase the rest of the folders and files recursively. – Peter Mar 17 '17 at 08:58
1

simply, you can rely on DirectoryIterator

The DirectoryIterator class provides a simple interface for viewing the contents of filesystem directories.

function removeDirectory($dir_to_erase) {
    $files = new DirectoryIterator($dir_to_erase);
    foreach ($files as $file) {
        // check if not . or ..
        if (!$file->isDot()) {
            $file->isDir() ? removeDirectory($file->getPathname()) : unlink($file->getPathname());
        }
    }
    rmdir($dir_to_erase);
    return;
}

there are lot of features there you may make use of them, as check the owner which is pretty useful to make sure not to remove critical file.

hassan
  • 7,013
  • 2
  • 20
  • 32
  • @Peter If this works like a charm, please award it the green tick so new readers don't come and try to solve a problem that is already solved, nor waste time reading other answers that may or may not work. – mickmackusa Mar 17 '17 at 09:15
0

As per this answer:

PHP glob() doesnt find .htaccess

glob(".*") will find .htaccess

Community
  • 1
  • 1
Jay Kay
  • 11
  • 3
  • Understood. Does that mean I have to change the line: $files = glob($dir_to_erase . '/*'); to $files = glob($dir_to_erase . '/.*'); ? – Peter Mar 17 '17 at 08:01
  • Tried that, and I got a bunch of errors - now nothing is erased: Warning: glob(): Pattern exceeds the maximum allowed length of 4096 characters in /home/chalouco/public_html/hihi/main/cleanup.php on line 20 Warning: glob(): Pattern exceeds the maximum allowed length of 4096 characters in /home/chalouco/public_html/hihi/main/cleanup.php on line 21 Warning: array_merge(): Argument #1 is not an array in /home/chalouco/public_html/hihi/main/cleanup.php on line 22 Warning: Invalid argument supplied for foreach() in /home/chalouco/public_html/hihi/main/cleanup.php on line 22 – Peter Mar 17 '17 at 08:16