2

So I'm creating an image upload site and I need to delete multiple directories and files simultaneously. I have managed to create code that does the job however I'm unsure whether this is 'good code' as I'm repeating myself.

Is there a better way to write the below?

$dirname = 'uploads/'.$album_id;
$dirnamethumb = 'uploads/thumbs/'.$album_id;

if (is_dir($dirname))
    $dir_handle = opendir($dirname);
if (!$dir_handle)
    return false;
while($file = readdir($dir_handle)) {
    if ($file != "." && $file != "..") {
        if (!is_dir($dirname."/".$file))
            unlink($dirname."/".$file);
        else
            delete_directory($dirname.'/'.$file);
    }
}

if (is_dir($dirnamethumb))
    $dir_handle = opendir($dirnamethumb);
if (!$dir_handle)
    return false;
while($file = readdir($dir_handle)) {
    if ($file != "." && $file != "..") {
        if (!is_dir($dirnamethumb."/".$file))
            unlink($dirnamethumb."/".$file);
        else
            delete_directory($dirnamethumb.'/'.$file);
    }
}
closedir($dir_handle);
rmdir($dirname);
rmdir($dirnamethumb);
return true;

Thank you in advance for your help!

Random
  • 3,158
  • 1
  • 15
  • 25
  • When you see a repetition, make a loop with your data : `$dirs = array('uploads/'.$album_id, 'uploads/thumbs/'.$album_id);`, then loop on `$dirs` to do your stuff... After, I don't know about the "good way" to delete a folder... there are many functions... yours looks clean. – Random Jul 08 '15 at 16:10
  • Amazing! It is now half the size, Thank you very much!!! – Simon Turner Jul 08 '15 at 16:38

2 Answers2

1

Why not try this recursive function from similar question

function rrmdir($dir) { 
  foreach(glob($dir . '/*') as $file) { 
    if(is_dir($file)) 
      rrmdir($file); 
    else 
      unlink($file); 
  } rmdir($dir); 
}
Community
  • 1
  • 1
Petko Kostov
  • 367
  • 1
  • 9
  • you may have to check if `$file` is not `.` or `..`, otherwise you will have an infinite recursive loop... – Random Jul 09 '15 at 07:12
0

try this,

$dir = '/path/to/some/dir/'; // notice: trailing slash!
if ($handle = opendir($dir)) {
    while (false !== ($entry = readdir($handle))) {
        if (is_dir($dir . $entry) ) {
            rmdir($dir . $entry);
        }
    }
    closedir($handle);
}
?>
  • Doesn't this only delete one directory though? I am looking to delete multiple directories at the same time. Hence why I have repeated the code – Simon Turner Jul 08 '15 at 15:49
  • Sorry, I don't think I have explained myself very well. Allow me to re-phrase: I am looking to delete certain directories within a parent directory. The user will have the option to delete a photo album which will be saved under 'upload/albumname' and the thumbnails for those same pictures will be saved under 'upload/thumbs/albumname'. Therefore I am only looking to delete the specified directories only. In my above code I loop through and delete all the files in one directory, then repeat the exact same code but with a different file path. – Simon Turner Jul 08 '15 at 16:05