Jump to content

Date range question


JPark

Recommended Posts

Hopefully an easy question...

 

I want a php page to (among other things) seach for any files more than a week old and, if they are there, delete them.

 

I am creating text files in a directory and the files are called "<date>file.txt, like 03252009file.txt.

 

So, if I have the following files -- 03252009file.txt, 03232009file.txt, 03192009file.txt, 03172009file.txt,

02252009file.txt and 02012009file.txt -- what is a good way to have php find and delete 03172009file.txt, 02252009file.txt and 02012009file.txt?

 

Thanks!

 

Joe

Link to comment
https://forums.phpfreaks.com/topic/151102-date-range-question/
Share on other sites

so something link this

<?php
$dir = "/path/to/files/";
if (is_dir($dir))
{
    if ($dh = opendir($dir))
{
        while (($file = readdir($dh)) !== false)
	{
		//if the file modified time + 1 week  is less than the time now then unlink
		if(filemtime($dir.$file)+(7*24*60*60) < time() )
		{
			echo "unlink(".$dir.$file.")<br>";
			#unlink($dir . $file); //uncomment to use
		}
        }
        closedir($dh);
    }
}
?>

 

EDIT: note this will not delete the files but only echo them.. check its correct first! ;)

Link to comment
https://forums.phpfreaks.com/topic/151102-date-range-question/#findComment-793791
Share on other sites

//if the file modified time + 1 week  is less than the time now then unlink
		if(filemtime($dir.$file)+(7*24*60*60) < time() )
		{
			echo "unlink(".$dir.$file.")<br>";
			#unlink($dir . $file); //uncomment to use
		}

 

Well, I created all the files today so the modified time still has today's date (not a week ago).  So, to test it, I need to have the script look for filenames that contain a date that is a week or more older.

 

I can do it but in a very lame way:

$sevendaysago = mktime(0,0,0,date("m"), date("d")-7, date("Y"));
$temp = date("mdY", $sevendaysago);
$sevendaysagofile = $temp.'notifyme.txt';

if (file_exists($sevendaysagofile)) {
unlink($sevendaysagofile);
} else {
echo "The file ". $sevendaysagofile. " does not exist<br /><br />";
}

and repeat that several times to catch a couple more days but I thought there would be a way to look for anything that contains a filename with 03182009 or earlier and delete that?

 

Joe

Link to comment
https://forums.phpfreaks.com/topic/151102-date-range-question/#findComment-793812
Share on other sites

just append a php timestamp to the filename instead of the actual date like

 

1238008891_filename.txt

 

Then you can explode the filename on the underscore, and use the first item in the array to test against... with a loop of something like

 

foreach($files as $filename) {
explode("_", $filename);
if (fileparts[0] < (time() - 60 * 60 * 24 * 7)) {
unlink($filename);
}
}

Link to comment
https://forums.phpfreaks.com/topic/151102-date-range-question/#findComment-793817
Share on other sites

This should work:

 

<?php
$dir = '/path/to/files/';
$files = glob("$dir*.txt");
foreach ($files as $file) {
$filename = basename($file);
//construct standard date (yyyy-mm-dd) from filename
$stddate = substr($filename, 4, 4) . '-' . substr($filename, 0, 2) . '-' . substr($filename, 2, 2);
if (strtotime("$stddate + 7 days") < time()) {
	//unlink($file);
	echo "To be deleted: $file<br />";
}
}
?>

 

Uncomment the unlink() function call to actually delete the files.

Link to comment
https://forums.phpfreaks.com/topic/151102-date-range-question/#findComment-793967
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.