Select Website 

Recruitment Directory's Blog - Australia's #1 Recruitment Technology Blog!

Back to Menu Back to Menu

PHP Code Snippet - Delete Batch Uploaded Job Files

Posted By: Thomas Shaw, 3:12pm Thursday 25 March 2010    Print Article

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

Comments Hide Comments (1)

Feel free to join in on the conversation. All comments are moderated before publishing. Comments posted by subscribers don't necessarily reflect the views of Recruitment Directory.

 Michael Specht (9:26am Friday 26 March 2010)

Given many people are now hosting on a LAMP platform you could just create a bash script with one line & get rid of all files older than 30 days

find /home/mydirectory -type f -mtime +30 -exec rm {} ;


Your Name: * Required
Your Email Address: * Required
Website URL:
Comments: * Required
Refresh
Enter the code you see in the image above (case sensitive). Click on the image to refresh it.
 


Back to Menu Back to Menu



Random Blog Articles

Job Board Statistics - Sep 09
Published: 1:37pm Monday 19 October 2009

Upcoming Webinar - Recruitment Rumble
Published: 9:08pm Thursday 30 April 2009

Using Twitter for Recruitment presentation slides
Published: 4:11pm Thursday 16 April 2009

Do not pay to post your jobs to Twitter
Published: 8:00am Monday 07 June 2010

RCSA website security will become an industry nightmare
Published: 3:48pm Thursday 05 February 2009