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
https://forums.phpfreaks.com/topic/57824-solved-cron-to-clear-a-folder/
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
            }
        }
    }
}
?>

Archived

This topic is now archived and is closed to further replies.

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