Delete files older than x days in php

Written by pelister. Posted in PHP

You may have situations where you want to delete files older than X number of days, but deleting them manually by checking date and time is not possible, and you might have completely avoided deleting the files which is no longer necessary resulting in growing heap of unwanted files in your web-server. Well I was in similar situation, before I became familiar with PHP, but now I always say – know a bit of PHP and it will make your life easier.

delete files in php

Well here we go, the PHP script that deletes the files that is X number of days old. First script, then a bit of explanation.

<?
$days = 1;
$dir = dirname ( __FILE__ );

$nofiles = 0;

    if ($handle = opendir($dir)) {
    while (( $file = readdir($handle)) !== false ) {
        if ( $file == '.' || $file == '..' || is_dir($dir.'/'.$file) ) {
            continue;
        }

        if ((time() - filemtime($dir.'/'.$file)) > ($days *86400)) {
			$nofiles++;
            unlink($dir.'/'.$file);
        }
    }
    closedir($handle);
	echo "Total files deleted: $nofiles \n";
}
?>

Now paste this code and save it as a php file, upload it to the folder from where you want to delete the files. You can see at the beginning of this php code

$days = 1;

that sets the number of days, for example if you set it to 2 then files older than 2 days will be deleted. Basically this is what happens when you run the script, gets the current directory and reads the file entries, skips ‘.’ for current directory and further checks if there are any other directories,

if ( $file == '.' || $file == '..' || is_dir($dir.'/'.$file) ) {
           continue;
}

if the file entry is not a directory then it fetches the file modified time (last modified time) and compares, if it is number of days old

if ((time() - filemtime($dir.'/'.$file)) > ($days *86400)) {
			$nofiles++;
            unlink($dir.'/'.$file);
}

if the condition becomes true then it deletes the file with the help of unlink( ) php function. Finally closes the directory and exits. I have also added a counter to count the number of files being deleted, which will be displayed at the end of deletion process. So place the php file in the directory that needs the file deletion and execute it.

1 Star2 Stars3 Stars4 Stars5 Stars (9 votes, average: 4.78 out of 5)
Loading...
pelister
3D modeler and Web designer. Founder of Techlister. Love to build sites using WordPress and Joomla. Interested in Astronomy, project member of SETI (Search for Extra Terrestrial Intelligence).

Tags: , , , ,

Comments (4)

  • Paul

    |

    I love this script. I’ve been using it for a month now, which brings me to my question. How can I have it delete all files in the directory, except for this php file? I have an image uploading site and run this periodically. Had it set to delete older than 30 days, but it ended up deleting itself!
    Thanks for this script, and your help!

    Reply

    • pelister

      |

      You can compare the filename ( $file ) with __FILE__ ( which contains the currently executing php filename and skip if both are same, you must do this before unlink().

      Reply

  • Gary

    |

    Great script with a working counter which I was looking for ages for.
    However, it doesn’t seem to delete files in sub-directories, which I need.

    Reply

    • pelister

      |

      Gary, You can edit this line

      $dir = dirname ( __FILE__ );

      to provide the required directory.

      Reply

Leave a comment

Techlister© - 2012