gerkintrigg Posted June 30, 2007 Share Posted June 30, 2007 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. Quote Link to comment Share on other sites More sharing options...
ignace Posted June 30, 2007 Share Posted June 30, 2007 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 } } } } ?> Quote Link to comment Share on other sites More sharing options...
Vikas Jayna Posted June 30, 2007 Share Posted June 30, 2007 How about using a simple linux command in cron: rm -rf <foldername>/* Quote Link to comment Share on other sites More sharing options...
gerkintrigg Posted June 30, 2007 Author Share Posted June 30, 2007 cool... so i can just use that linux code in the cron? and can i just add php files to the cron to execute those then? with the linux code - do i need to have the whole path in the foldername tag? Quote Link to comment Share on other sites More sharing options...
Vikas Jayna Posted July 2, 2007 Share Posted July 2, 2007 There is no need of adding any php files. Here is the crontab code for executing the command at 00:00 hrs everyday 00 00 * * * /bin/rm -rf <foldername> And yes the complete path to the folder has to be given. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.