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.
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.
PHP Delete all files older than x days in 2 folders
Your function definition is out of a class so you need to remove PUBLIC
public function deleteFrom($path){
to
function deleteFrom($path){
Also $this->
is not correct you need to remove them and call the function as
deleteFrom($pasta);
Check here how Public, Private etc are used in PHP
http://php.net/manual/en/language.oop5.visibility.php
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 folder and contents older than 2 days PHP
As @Dagon pointed out you need to verify the date of the file object before deleting.
Add this to your foreach
. The condition will ask if the date of the file object is smaller than the current time minus 60 seconds * 60 (minutes) * 24 (hours) * 2 (days).
foreach($structure as $file) {
if (filemtime($file) < time() - (60 * 60 * 24 * 2) ) {
if (is_dir($file)) recursiveRemove($file);
elseif (is_file($file)) unlink($file);
}
}
Note that your recursive function will not delete files that do not match the condition ( they are newer than 2 days) inside directories that do match the condition (older than 2 days).
Making rmdir
throw a warning if it's not empty.
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.
How to delete files older than X hours
Does your find
have the -mmin
option? That can let you test the number of mins since last modification:
find $LOCATION -name $REQUIRED_FILES -type f -mmin +360 -delete
Or maybe look at using tmpwatch
to do the same job. phjr also recommended tmpreaper
in the comments.
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);
}
Related Topics
PHP Exec() Is Not Executing the Command
Where Exactly Do I Put a Session_Start
How to Work with Many-To-Many Relations in Yii2
Creating a Soap Call Using PHP with an Xml Body
How to Implement MVC from Scratch in PHP
How to Remove a Key and Its Value from an Associative Array
Generating a Waveform Using Ffmpeg
How to Post Multiple <Input Type="Checkbox" /> as Array in PHP
Laravel: Auth::User()->Id Trying to Get a Property of a Non-Object
What Are the Differences of System(), Exec() and Shell_Exec() in PHP
Whats the Best Way to Do User Authentication in PHP
Get All the Images from a Folder in PHP
How to Make My PHP Ide Understand Dependency Injection Containers