Login     Register
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    Email Article    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: multi posting system applicant tracking system recruitment database software bulk jobs ftp upload job board software job board code snippet php 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
 


Back to Menu Back to Menu



Random Blog Articles

Has your job expired?
Published: 12:14pm Tuesday 16 June 2009

jobs.govt.nz igovt login system
Published: 5:30pm Tuesday 15 December 2009

Mobile Device Statistics & Mobile Application behaviour. AdMob Mobile Metrics report
Published: 10:13pm Tuesday 02 March 2010

Hide and Seek
Published: 3:57pm Sunday 15 February 2009

Importing Contact Lists from Social Networking sites
Published: 12:00am Wednesday 14 October 2009


Newsletter Mailing List

Stay informed of current news, upcoming events and promotional offers.

Top 25 Most Influential

Latest Blog Comments

Niels Bramsen - 10:05pm Thursday 09 September 2010
Using the LinkedIn API

saudi jobs - 5:51am Tuesday 07 September 2010
Log in with LinkedIn

frustrated jobseeker - 1:27pm Tuesday 31 August 2010
Are you a Social Recruitment wanker?

Recruitment Software - 10:24pm Thursday 26 August 2010
IT Video Resume - Killa Appz

Jason - 11:16am Wednesday 18 August 2010
IT Video Resume - Killa Appz

David Lyons - 5:17pm Friday 13 August 2010
Plan for the worst

DmitryK - 6:06pm Monday 09 August 2010
OWASP Top 10 and your Recruitment Website - Part 1

Jim Manico - 5:28pm Monday 09 August 2010
OWASP Top 10 and your Recruitment Website - Part 1

Steve Ludlow - 12:28pm Friday 06 August 2010
Are you a Social Recruitment wanker?

Brad Stewart - 10:45am Tuesday 03 August 2010
2010 Email Marketing Benchmark Report - HR/Recruitment Sector

Upcoming Webinars