Griffith Designs

PHP Empty a Folder

by webmasterjunkie on Oct.30, 2010, under Designs, Internet, PHP, Programming, Service, Web

Well been working on some cron jobs for my latest site, and had to find a way to dump all the files in the temporary folder. If you are looking to do this in PHP, here you go:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
function empty_directory_folder ($dir, $deleteMe) {
    if (!$dh = @opendir ($dir)) return;
    while (false !== ($obj = readdir($dh))) {
        if($obj == '.' || $obj == '..') continue;
        if (!@unlink ($dir . '/' . $obj)) empty_directory_folder ($dir . '/' . $obj, true);
    }
    closedir ($dh);
    if ($deleteMe){
        @rmdir ($dir);
    }
}
// Call the function like this
// You can change the second argument to true if you want to delete the folder too
empty_directory_folder ('../tmp', false);
:,

Leave a Reply

You must be logged in to post a comment.

Looking for something?

Use the form below to search the site:

Still not finding what you're looking for? Drop a comment on a post or contact us so we can take care of it!

Connect