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.


$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))
	  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 />';
	  } //end for 
    } // end if
  }  // end if
// END 

Article URL:

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
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 - February 2010
Published: 4:12pm Wednesday 03 March 2010

Hopeless romantic seeking Valentines date
Published: 12:21am Friday 20 February 2009

Advanced Twitter use for Recruitment
Published: 4:58pm Wednesday 27 May 2009

Cut the fat. Application Form Filename Structure
Published: 1:18am Friday 27 November 2009

Job ad of the month - I'm tired of writing boring adverts for boring Recruitment Consultants
Published: 9:26pm Monday 05 July 2010