[英]How to delete files from directory based on creation date in php?
我有一个存储 html 文件的缓存文件夹。 它们在需要时会被覆盖,但很多时候,很少使用的页面也会缓存在那里,最终会占用空间(5 周后,驱动器已满,缓存文件超过 270 万个)。
循环遍历包含数十万个文件的目录并删除早于 1 天的文件的最佳方法是什么?
我认为您可以通过使用 readdir 循环遍历目录并根据时间戳删除来解决此问题:
<?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);
}
?>
if((time() - $filelastmodified) > 24*3600)
将选择早于 24 小时(24 小时乘以每小时 3600 秒)的文件。 如果您想要天数,对于超过一周的文件,它应该读取例如 7*24*3600。
另外,请注意filemtime
返回文件的最后修改时间,而不是创建日期。
它应该是
if((time()-$filelastmodified) > 24*3600 && is_file($file))
以避免错误.
和..
目录。
以下函数根据文件的创建日期列出文件:
private function listdir_by_date( $dir ){
$h = opendir( $dir );
$_list = array();
while( $file = readdir( $h ) ){
if( $file != '.' and $file != '..' ){
$ctime = filectime( $dir . $file );
$_list[ $file ] = $ctime;
}
}
closedir( $h );
krsort( $_list );
return $_list;
}
例子:
$_list = listdir_by_date($dir);
现在您可以遍历列表以查看它们的日期并相应地删除:
$now = time();
$days = 1;
foreach( $_list as $file => $exp ){
if( $exp < $now-60*60*24*$days ){
unlink( $dir . $file );
}
}
尝试拆分器
// setup timezone and get timestamp for yesterday
date_default_timezone_set('Europe/Berlin'); // change to yours
$yesterday = strtotime('-1 day', time());
// setup path to cache dir and initialize iterator
$path = realpath('/path/to/files'); // change to yours
$objects = new RecursiveIteratorIterator(
new RecursiveDirectoryIterator($path));
// iterate over files in directory and delete them
foreach($objects as $name => $object){
if ($object->isFile() && ($object->getCTime() < $yesterday)) {
// unlink($object);
echo PHP_EOL, 'deleted ' . $object;
}
}
/* Detele Cache Files Here */
$dir = "cache/"; /** define the directory **/
/*** cycle through all files in the directory ***/
foreach (glob($dir."*") as $file) {
//foreach (glob($dir.'*.*') as $file){
/*** if file is 24 hours (86400 seconds) old then delete it ***/
if (filemtime($file) < time() - 3600) { // 1 hour
unlink($file);
}
}
我正在使用这个,希望它有所帮助。
只是要注意戈登的时间比较(见上文: https : //stackoverflow.com/a/2205833/1875965 )是与“天”而不是“24小时”比较时唯一正确的,因为并非所有天都有24小时(夏季/冬季等)。
例如使用
// setup timezone and get timestamp for yesterday
date_default_timezone_set('Europe/Berlin'); // change as appropriate
$yesterday = strtotime('-1 day', time());
比较文件日期时。
这可能不是一个大问题,但是当您处理周/月等时可能会导致意外行为。我发现最好坚持使用上述方法,因为它会使任何涉及日期/时间的过程保持一致并避免困惑。
还要检查文件日期的时区,因为有时 PHP 的默认时区与系统时区不同。
亲切的问候,桑德拉。
$directory = $_SERVER['DOCUMENT_ROOT'].'/pathfromRoot/';
$files = array_slice(scandir($directory), 2);
foreach($files as $file)
{
// $extension = substr($file, -3, 3);
// if ($extension == 'jpg') // in case you only want specific files deleted
// {
$stat = stat($directory.$file);
$filedate = date_create(date("Y-m-d", $stat['ctime']));
$today = date_create(date("Y-m-d"));
$days = date_diff($filedate, $today, true);
// dd($days);
if ($days->days > 180)
{
unlink($directory.$file);
}
// }
}
通过更改@pawel 的解决方案,我在下面创建了函数。 起初我忘记在文件名中添加“路径”,这需要我半个小时才能找到。
public function deleteOldFiles ($hours=24) {
$path='cache'.DS;
if ( $handle = opendir( $path ) ) {
while (false !== ($file = readdir($handle))) {
$filelastmodified = filemtime($path.$file);
if((time()-$filelastmodified) > 24*3600 && is_file($path.$file))
{
unlink($path.$file);
}
}
closedir($handle);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.