natasha_thomas Posted May 19, 2010 Share Posted May 19, 2010 Friends, I want a way/script/code to auto delete a folder content (Folder name is "Gallary") I mean i want to Empty the Folder "GAllary", in each X no of days. How can i achieve this, any cron Job Script or Code? May someone help? (I am on hostgator Sharedhosting... So i think its Unix Server with Cpanel) Quote Link to comment https://forums.phpfreaks.com/topic/202253-how-auto-delete-a-folder-content-by-cron-job-after-no-of-days/ Share on other sites More sharing options...
tomfmason Posted May 19, 2010 Share Posted May 19, 2010 this is how you setup a cron with hostgator - http://support.hostgator.com/articles/cpanel/how-do-i-create-and-delete-a-cron-job and here is a php snippet that empties a directory- http://snippets.dzone.com/posts/show/5004 Quote Link to comment https://forums.phpfreaks.com/topic/202253-how-auto-delete-a-folder-content-by-cron-job-after-no-of-days/#findComment-1060545 Share on other sites More sharing options...
ignace Posted May 19, 2010 Share Posted May 19, 2010 I have dived into Linux permissions and to remove a file/directory you need the write permission. The below script should do the job and report if something isn't correct. $directoryPath = realpath('path/to/directory'); if (!is_dir($directoryPath)) { error_log("Directory $directoryPath does not exist."); exit(0); } function cleanDirectory($directoryPath, $recursive = FALSE) { if (!is_readable($directoryPath) || !is_writable($directoryPath)) { error_log("Can't delete directory $directoryPath as you don't have the correct permissions."); return FALSE; } $folderEmpty = TRUE; foreach (scandir($directoryPath) as $file) { $filePath = realpath("$directoryPath/$file"); if (is_dir($filePath)) { if (!cleanDirectory($filePath, $recursive)) { $folderEmpty = FALSE; } continue; } if (!deleteFile($filePath)) { $folderEmpty = FALSE; } } return $folderEmpty ? @rmdir($directoryPath) : FALSE; } function deleteFile($filePath) { if (!is_writable($filePath)) { error_log("Can't delete file $filePath as you don't have the correct permissions."); return FALSE; } return @unlink($filePath); } Quote Link to comment https://forums.phpfreaks.com/topic/202253-how-auto-delete-a-folder-content-by-cron-job-after-no-of-days/#findComment-1060553 Share on other sites More sharing options...
natasha_thomas Posted May 19, 2010 Author Share Posted May 19, 2010 I have dived into Linux permissions and to remove a file/directory you need the write permission. The below script should do the job and report if something isn't correct. $directoryPath = realpath('path/to/directory'); if (!is_dir($directoryPath)) { error_log("Directory $directoryPath does not exist."); exit(0); } function cleanDirectory($directoryPath, $recursive = FALSE) { if (!is_readable($directoryPath) || !is_writable($directoryPath)) { error_log("Can't delete directory $directoryPath as you don't have the correct permissions."); return FALSE; } $folderEmpty = TRUE; foreach (scandir($directoryPath) as $file) { $filePath = realpath("$directoryPath/$file"); if (is_dir($filePath)) { if (!cleanDirectory($filePath, $recursive)) { $folderEmpty = FALSE; } continue; } if (!deleteFile($filePath)) { $folderEmpty = FALSE; } } return $folderEmpty ? @rmdir($directoryPath) : FALSE; } function deleteFile($filePath) { if (!is_writable($filePath)) { error_log("Can't delete file $filePath as you don't have the correct permissions."); return FALSE; } return @unlink($filePath); } Thanks IGnace... Where can i set the Time i mean seconds or no of days after while the Folder should be emptied automatically....? Quote Link to comment https://forums.phpfreaks.com/topic/202253-how-auto-delete-a-folder-content-by-cron-job-after-no-of-days/#findComment-1060651 Share on other sites More sharing options...
natasha_thomas Posted May 19, 2010 Author Share Posted May 19, 2010 this is how you setup a cron with hostgator - http://support.hostgator.com/articles/cpanel/how-do-i-create-and-delete-a-cron-job and here is a php snippet that empties a directory- http://snippets.dzone.com/posts/show/5004 Thank you!!! I appreciate!!! was curious to know, Where can i set the Time i mean seconds or no of days after while the Folder should be emptied automatically....? Quote Link to comment https://forums.phpfreaks.com/topic/202253-how-auto-delete-a-folder-content-by-cron-job-after-no-of-days/#findComment-1060652 Share on other sites More sharing options...
ignace Posted May 19, 2010 Share Posted May 19, 2010 Use CRON to set when my script should execute. You will be able to find it in your CPanel. Test it first though like I already mentioned in the PM. Quote Link to comment https://forums.phpfreaks.com/topic/202253-how-auto-delete-a-folder-content-by-cron-job-after-no-of-days/#findComment-1060661 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.