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

OWASP Top 10 and your Recruitment Website - Part 1
Published: 8:30am Monday 09 August 2010

Cut the fat. CAPTCHA
Published: 11:32pm Monday 18 January 2010

Surviving a Redundancy - 10 strategies to get yourself and your career back on track
Published: 3:34pm Tuesday 19 May 2009

How much do I charge candidates for work experience?
Published: 12:11pm Wednesday 08 June 2011

Are you looking for the perfect role this Christmas
Published: 9:00am Friday 12 December 2008