Recruitment Directory's Blog - Australia's #1 Recruitment Technology Blog!
If you manage any type of job board, there is a high chance you have some sort of 3rd party integration with your recruitment database software, applicant tracking system or multi posting system. This PHP code snippet will help developers who accept batch job uploaded files via FTP.
Over time, if your job board receives a new upload file every hour you will end up with hundreds (if not thousands) of old files taking up space on your server. For example, if you receive a new file every hour that is 250kb, every day you will add 5.85mb of files. After 7 days this would have grown to 41mb.
You can manage this by logging into your system every week, and deleting the old files manually which takes time or automate the process.
This code below cycles through a specified directory, sorts the oldest files, and if there are more files in the directory than specified, it will remove the oldest file.
If you have a better solution, let me know.
// KEEP THE LATEST 5 FILES, DELETE THE OLD FILES
clearstatcache();
$filestats1 = array();
$directoryname = $rootpath.'/batchupload';
$numberfiles = 5;
if($dir = @opendir($directoryname))
{
while (false !== ($file = readdir($dir)))
{
if(substr($file,0,1) != '.') // specify filename extension
{
if($stats = @stat($directoryname.$file))
{
$filestats1[] = array(
'file' => $file,
'mtime' => $stats['mtime'],
'ctime' => $stats['ctime'],
'error' => false);
} else {
$filestats1[] = array('file' => $file, 'error' => true);
} // end if
} // end while
} // end while
if(!empty($filestats1) && is_array($filestats1))
{
@sort($filestats1);
echo 'FILES IN FOLDER - '.count($filestats1).'<br />';
$ii = (count($filestats1) - $numberfiles);
for($i = 0; $i < $ii; $i++)
{
echo 'DELETED OLD FILE - '.$filestats1[$i]['file'].'<br />';
@unlink($directoryname.$filestats1[$i]['file']);
} //end for
} // end if
} // end if
// END
Article URL: http://www.recruitmentdirectory.com.au/Blog/php-code-snippet-delete-batch-uploaded-job-files-a343.html
Article Tags: php code snippet job board job board software bulk jobs ftp upload recruitment database software applicant tracking system multi posting system batch job upload Hide Comments (1)
find /home/mydirectory -type f -mtime +30 -exec rm {} ;