How To: Delete All Files In A Directory With PHP

I had a nagging issue that started a few weeks ago when I get a lot of email with large attachments (see my last post about emails from my aunt); when I deleted emails from my device the messages are not deleted from the server as they were with my Windows Mobile device. BlackBerry devices do not allow you to setup the mail account yourself (not to any degree of specificity anyways..), so each time I add the account, it is setup as IMAP. The issue is that when I delete items from my device, they are sent to the .Trash folder, and never deleted from the server. This becomes an issue with a small mailbox quota (mine are set to 25mb because I use POP3 on my laptop and delete the messages from the server after they’re downloaded by Thunderbird) which causes the server to reject mail when the mailbox is full.

Surely there is a simple solution, which is to raise the mailbox quota. However, all that does is allow more and more mail to pile up, which would still require me to go in and clear out the trash folder every few weeks (or days, depending on the size of the emails I receive..). So I did some searching and found a nice little snippet of PHP to tackle this issue:

<?
define
('PATH''/home/user/mail/domain.com/mailaccount/.Trash/cur/');
function 
destroy($dir) {
    
$mydir opendir($dir);
    while(
false !== ($file readdir($mydir))) {
        if(
$file != "." && $file != "..") {
            
chmod($dir.$file0777);
            if(
is_dir($dir.$file)) {
                
chdir('.');
                
destroy($dir.$file.'/');
                
rmdir($dir.$file) or DIE("couldn't delete $dir$file<br />");
            }
            else
                
unlink($dir.$file) or DIE("couldn't delete $dir$file<br />");
        }
    }
    
closedir($mydir);
}
destroy(PATH);

echo 'all done.'
?>

I placed this into a PHP file in a folder above public_html (so it’s not accessible by the public) and setup a cronjob to execute this code once every hour for each mailbox that I use on my BlackBerry. This prevents the mailbox from filling up as it deletes the trash every hour and doesn’t require me to check it every so often. :)

Filed under: php, site, tutorial — Tags: , , , , , , , ,

Tutorial: PHP – explode()

I've recently discovered the power of explode(), and it's a powerful little tool that anyone using PHP should know about.

PHP:
  1. <?
  2. $var1 = "apples oranges bananas pears grapes";
  3. $fruit = explode(" ", $var1);
  4. foreach($fruit AS $echo){
  5.    echo "$echo<br />\n";
  6.    }
  7. ?>

This will result in the following output:

CODE:
  1. apples<br />
  2. oranges<br />
  3. bananas<br />
  4. pears<br />
  5. grapes<br />

When using the explode() function, you fill it in in the following manner:

PHP:
  1. $name = explode("__DELIMITER__", __$VARIABLE__);

__DELIMITER__ is what you will explode, or split by. In the example above I used " " (space).

__$VARIABLE__ identifies which string to split. This should be defined before the explode.

In the example above, I used space as the delimiter, and I exploded $var1. This will split the string at each space, and assign each split of the string to a new variable, which is defined before the explode(). In the example, I used $fruit. Each new variable is then assigned as $fruit[0], $fruit[1], $fruit[2], etc. The array starts at zero, and stops when there is no more data to split.

The second part of the example echos the array.

PHP:
  1. foreach($fruit AS $echo){
  2.    echo "$echo<br />\n";
  3.    }

foreach() loops through all of the items in the array, and echos the code betwen { and } for each item. In this case, each item is echoed, followed by <br />. As you can see by the resulting code, each item from the original variable is echoed nicely, on it's own line.

You can play around with different strings and delimiters, and the results are pretty much endless

For more information, view the official PHP.net page for explode().

Filed under: php, tutorial — Tags: , ,