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 the old files from a directory if a condition is true in PHP?
Basic script to give you the idea. Push all the files with their times into an array, sort it by descending time order and walk trough. if($count > 10)
says when the deletion should start, i.e. currently it keeps the newest 10.
<?php
$directory = ".";
$files = array();
foreach(scandir($directory) as $file){
if(is_file($file)) {
//get all the files
$files[$file] = filemtime($file);
}
}
//sort descending by filemtime;
arsort($files);
$count = 1;
foreach ($files as $file => $time){
if($count > 10){
unlink($file);
}
$count++;
}
Delete old files - an hour ago or more than an hour ago
You can use filemtime()
or filectime()
. In addition, you can't use rmdir()
, because you want to delete specific files.
What you have to do
date("U",filectime($file)
- return the last time your file was modified in Unix Epoch time.unlink($file)
- deletes the specified file. You have to use it instead ofrmdir()
.An
if
andwhile
/for
statement is needed:while($file)
{
if(date("U",filectime($file) <= time() - 3600)
{
unlink($file)
}
}
Then add it to your script:
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(date("U",filectime($file) >= time() - 3600)
{
unlink($dir.$file)
}
}
}
else
unlink($dir.$file) or DIE("couldn't delete $dir$file<br />");
}
}
closedir($mydir);
}
I hope it helps you.
Remember:
- There is no way to get the file creation time in Unix/Php
- The current
if
statement returnstrue
if the file was last changed more or equal to one hour ago
How to remove files/folders older than a certain time
This construct will delete files older than 60 minutes (3600 seconds) using the filemtime()
function:
if (filemtime($object) < time() - 3600) {
// Remove empty directories...
if (is_dir($object)) rmdir($object);
// Or delete files...
else unlink($object);
}
Note that for rmdir()
to work, the directory must be empty.
Delete file with past date in name in PHP
You've made a rod for your back with this naming scheme (it's also generally bad practice to have files with spaces in as far as possible), but no matter.
What you need to do is:
Scan the target directory using scandir or similar. (glob might be a better bet depending on the contents of the directory.)
Convert each valid (i.e.: not "." or "..") html file name into a timestamp via strtotime after using str_replace to get rid of the ".html" portion.
unlink the file if it's in the past.
How to delete all files in folder after specific time
You can execute the action as cronjob.
Read the following link: Crontab
You also can save a date in a database and consult in top of the script if 6 month have elapsed.
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.
Related Topics
Mysqli Binding Params Using Call_User_Func_Array
How to Set Selected Value of HTML Select Box with PHP
How to Display Only One Country or a Specific Area in Google Maps Using the API
How to Read Gzip-Ed Response from Stackoverflow API in PHP
PHP Header Redirect in a New Window
Adding Subscribers to a List Using Mailchimp's API V3
What Does Using a Single Pipe '|' in a Function Argument Do
Accessing Session from Twig Template
Get the First Letter of Each Word in a String
Get Value of Dynamically Chosen Class Constant in PHP
Load Data Local Infile Forbidden In... PHP
Replacing Placeholder Variables in a String
Simple HTML Dom File_Get_HTML Not Working - Is There Any Workaround