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!

2
  • 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. Commented Jul 8, 2015 at 16:10
  • Amazing! It is now half the size, Thank you very much!!! Commented Jul 8, 2015 at 16:38

2 Answers 2

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); 
}
Sign up to request clarification or add additional context in comments.

1 Comment

you may have to check if $file is not . or .., otherwise you will have an infinite recursive loop...
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);
}
?>

2 Comments

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
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.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.