Jump to content

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.