Jump to content

[SOLVED] Cron to clear a folder


gerkintrigg

Recommended Posts

Hi. I'd like to create a cron to empty a folder of temporary images and just wondered how to go about doing it. I'm happy with how to work the cron tab... but do i just write a script and point the cron to it?

 

What would the script look like to clear a folder of ALL files?

 

Do I need to think about security issues too? Thanks.

Link to comment
Share on other sites

You are experienced with cron so just call this snippet, buf before you do use this,

add the absolute folder path in sFolder!

 

<?php

// Folder !!!NO TRAILING SLASH!!!
$sFolder = '[folder comes here]';

// Delete all files within the folder
DeleteRecursive($sFolder);

// Function
function DeleteRecursive($sFolder) {
    // open folder
    $pFolder = opendir($sFolder);

    // read through folder
    while (false != ($sFile = readdir($pFolder))) {
        $sAbsolute = $sFolder . "/" . $sFile;
        if ($sFile != "." && $sFile != "..") {
            if (is_file($sAbsolute)) {
                unlink($sAbsolute);
            } else { // Directory assumed
                DeleteRecursive($sAbsolute); // Remove all files within the folder
                rmdir($sAbsolute); // Remove the folder
            }
        }
    }
}
?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.