PHP Script to Delete Files Older Than 24 Hrs, Deletes All Files

php script to delete files older than 24 hrs, deletes all files

(time()-filectime($path.$file)) < 86400

If the current time and file's changed time are within 86400 seconds of each other, then...

 if (preg_match('/\.pdf$/i', $file)) {
unlink($path.$file);
}

I think that may be your problem. Change it to > or >= and it should work correctly.

The correct way to delete all files older than 2 days in PHP

You should add an is_file() check, because PHP normally lists . and .., as well as sub-directories that could reside in the the directory you're checking.

Also, as this answer suggests, you should replace the pre-calculated seconds with a more expressive notation.

<?php
$files = glob(cacheme_directory()."*");
$now = time();

foreach ($files as $file) {
if (is_file($file)) {
if ($now - filemtime($file) >= 60 * 60 * 24 * 2) { // 2 days
unlink($file);
}
}
}
?>

Alternatively you could also use the DirectoryIterator, as shown in this answer. In this simple case it doesn't really offer any advantages, but it would be OOP way.

How to delete files from directory based on creation date in php?

I think you could go about this by looping through the directory with readdir and delete based on the timestamp:

<?php
$path = '/path/to/files/';
if ($handle = opendir($path)) {

while (false !== ($file = readdir($handle))) {
$filelastmodified = filemtime($path . $file);
//24 hours in a day * 3600 seconds per hour
if((time() - $filelastmodified) > 24*3600)
{
unlink($path . $file);
}

}

closedir($handle);
}
?>

The if((time() - $filelastmodified) > 24*3600) will select files older than 24 hours (24 hours times 3600 seconds per hour). If you wanted days, it should read for example 7*24*3600 for files older than a week.

Also, note that filemtime returns the time of last modification of the file, instead of creation date.

Php - Delete files older than 7 days for multiple folders

The simple solution, call the function after setting the parameter not after setting all the possible parameters into a scalar variable.

$days = 7;

$path = 'C:/Users/Legion/AppData/Local/Temp';
deleteOlderFiles($path,$days);

$path = 'C:/Users/Legion/AppData/Local/Temp/bla';
deleteOlderFiles($path,$days);

$path = 'C:/Users/Legion/AppData/Local/Temp/blabla';
deleteOlderFiles($path,$days);

$path = 'C:/Users/Legion/AppData/Local/Temp/blalbalba';
deleteOlderFiles($path,$days);

Alternatively, place the directories in an array and then call the funtion from within a foreach loop.

$paths = [];
$paths[] = 'C:/Users/Legion/AppData/Local/Temp';
$paths[] = 'C:/Users/Legion/AppData/Local/Temp/bla';
$paths[] = 'C:/Users/Legion/AppData/Local/Temp/blabla';
$paths[] = 'C:/Users/Legion/AppData/Local/Temp/blalbalba';

$days = 7;

foreach ( $paths as $path){
deleteOlderFiles($path,$days);
}

Delete all files from folder older than a specified amount of time with custom function

try:

function destroy($dir) {
$mydir = opendir($dir);
while($file = readdir($mydir)) {
if($file != "." && $file != "..") {
chmod($dir.$file, 0777);
if(is_dir($dir.$file)) {
chdir('.');
while($dir.$file) {
if((time() - filemtime($file)) > 3600) {
unlink($dir.$file);
echo $file.' had been deleted';
}
}
} else
if((time() - filemtime($file)) > 3600) {
unlink($dir.$file) or DIE("couldn't delete $dir$file<br />");
}
}
}
closedir($mydir);
}

Delete a file having specific age in PHP

Here is code for you

<?php
$dir = '/path/to/my/dir';
$files = scandir($dir);
$cnt = count($files);
$deadline = strtotime('now')-36000;
for($i = 0; $i < $cnt; ++$i)
{
$files[$i] = $dir.'/'.$files[$i];
if(!is_file($files[$i]) || $files[$i] == '.' ||
$files[$i] == '..' || filemtime($files[$i]) <= $deadline)
unset($files[$i]);
else
unlink($files[$i]);
}

Delete all images older than 1 hour

You forgot to close the date()

 if(date("U",filectime($file)) >= time() - 3600)
-------^

and missed a semi colon here

unlink($dir.$file);
-----^

Auto delete all files after x-time

Response for last comment from my first answer. I'm going to write code sample, so I've created another answer instead of addition one more comment.

To remove files with custom extension you have to implement code:

<?php
$path = dirname(__FILE__).'/files';
if ($handle = opendir($path)) {

while (false !== ($file = readdir($handle))) {
if ((time()-filectime($path.'/'.$file)) < 86400) { // 86400 = 60*60*24
if (preg_match('/\.txt$/i', $file)) {
unlink($path.'/'.$file);
}
}
}
}
?>

Comment: 1. This example uses regular expression /\.txt$/i, which means, that only files with extension txt will be removed. '$' sign means, that filename has to be ended with string '.txt'. Flag 'i' indicates, that comparison will be case-insensitive. More about preg_match() function.

Besides you can use strripos() function to search files with certain extension. Here is code snippet:

<?php
$path = dirname(__FILE__).'/files';
if ($handle = opendir($path)) {

while (false !== ($file = readdir($handle))) {
if ((time()-filectime($path.'/'.$file)) < 86400) { // 86400 = 60*60*24
if (strripos($file, '.txt') !== false) {
unlink($path.'/'.$file);
}
}
}
}
?>

Comment: This example seems more obvious. Result of strripos() also can be achieved with a combining of two functions: strrpos(strtolower($file), '.txt'), but, IMHO, it's a good rule to use less functions in your code to make it more readable and smaller. Please, read attentively warning on the page of strripos() function(return values block).

One more important notice: if you're using UNIX system, file removing could fail because of file permissions. You can check manual about chmod() function.

Good luck.



Related Topics



Leave a reply



Submit