deleting one day old files from folder using php

deleting one day old files from folder using php

We given php  code for Checking directory and deleting one day old files from folder using php, from folder in windows or linux through php language. For deleting the files from folder in any system (windows or linux). I created the PHP script for deleting the old files from system.

Earlier I created the script for deleting files from folder but that was for only linux. Then I created script which will work independently. Following script will work in mac or windows or linux.

deleting one day old files from folder using php

 /***************** Delete one day old files ****************/
 $dir = "C://Inetpub/vhosts/yourdomain/tmpfiles";

 if (is_dir($dir)) {
 if ($dh = opendir($dir)) {
 while ($file = readdir($dh)) {
 if(!is_dir($dir.$file)) {
 if (filemtime($dir.DIRECTORY_SEPARATOR.$file) <     strtotime('-1 days')) { //if 1 days old, delete
<div id=":1o">echo "Deleting $dir.$file (old)\n";
 unlink($dir.$file);
 }
 }
 }
 } else {
echo "ERROR. Could not open directory: $dir\n";
 }
 } else {
 echo "ERROR. Specified path is not a directory: $dir\n";
 }
 closedir($dh);

// Above script is platform independent.</div>

Published by

Purab

I am Purab from India, Software development is my profession and teaching is my passion. Programmers blog dedicated to the JAVA, Python, PHP, DevOps and Opensource Frameworks. Purab's Github Repo Youtube Chanel Video Tutorials Connect to on LinkedIn

One thought on “deleting one day old files from folder using php”

  1. Shouldn’t your less than be a greater than in the line:

    if (filemtime($dir.DIRECTORY_SEPARATOR.$file) < strtotime('-1 days')) { //if 1 days old, delete

    Just wondering. Great code btw.

Leave a Reply

Your email address will not be published.