就是现在我有一个存放日志的文件夹,需要怎么做才能用程序做到将较早的日志文件进行删除?大神们有方法么?
回复讨论(解决方案)
foreach(glob('日志文件目录/*.*') as $fn) { if(filemtime($fn) < 指定的时间) unlike($fn);}
可以写个脚本删呢,一天跑一次检查过期(比对当前时间和日志的最后修改时间或者创建时间)的日志文件给予删除
脚本的话比如这样,删除内容修改超过7天的文件
find ./ -mtime +7 | xargs rm -rf
是要用php完成这个工作呢,就是我有百度看有提供将文件夹里面的文件按照时间进行排序,但是我使用了都是不可以的。。。
这是我的代码
function dir_size($dir,$url){
$dh = @opendir($dir); //打开目录,返回一个目录流
$return = array();
$i = 0;
while($file = @readdir($dh)){ //循环读取目录下的文件
if($file!='.' and $file!='..'){
$path = $dir.'/'.$file; //设置目录,用于含有子目录的情况
if(is_dir($path)){
}elseif(is_file($path)){
$filesize[] = round((filesize($path)/1024),2);//获取文件大小
$filename[] = $path;//获取文件名称
$filetime[] = date("Y-m-d H:i:s",filemtime($path));//获取文件最近修改日期
$return[] = $url.'/'.$file;
}
}
}
@closedir($dh); //关闭目录流
array_multisort($filetime,SORT_DESC,SORT_STRING, $files);//按时间排序
return $return; //返回文件
}
dir_size("htdocs","htdocs");
?>
运行后出现问题
array_multisort(): Argument #1 is expected to be an array or a sort flag
最好的结果是我希望php运行后能将文件按照时间的顺序进行排列,这样我就能用函数有选择性的进行文件的删除。。。
学习了。。。。
array_multisort($filetime,SORT_DESC,SORT_STRING, $files);//按时间排序
$files 在哪里赋值的?
array_multisort($filetime,SORT_DESC,SORT_STRING, $files);//按时间排序
$files 在哪里赋值的?
我查了下函数的用法,知道了array_multisort函数的用法,最后一个给的参数应该是一个数组,但是我不知道怎么给第四个参数赋值。$files应该是指文件里面的所有文件,我不知道怎么去使用。。$files=$return[] = $url.'/'.$file;?
我把$files改成了$return还是没有用。。。
函数可改写为
function dir_size($dir,$url){ $dh = @opendir($dir); //打开目录,返回一个目录流 $return = array(); $i = 0; while($file = @readdir($dh)){ //循环读取目录下的文件 if($file!='.' and $file!='..'){ $path = $dir.'/'.$file; //设置目录,用于含有子目录的情况 if(is_dir($path)){ }elseif(is_file($path)){ $files[] = array( 'filename' => $path,//获取文件名称 'filesize' => round((filesize($path)/1024),2),//获取文件大小 'filetime' => date("Y-m-d H:i:s",filemtime($path)),//获取文件最近修改日期 'return' => $url.'/'.$file ); $filetime[] = filemtime($path);//获取文件最近修改日期 } } } @closedir($dh); //关闭目录流 array_multisort($filetime,SORT_DESC,SORT_STRING, $files);//按时间排序 return $files; //返回文件}
恩恩,我用了您的代码,发现可以使用的。
然后我发现还有一种方式就是直接把$files放在替换成$return,因为之前的数据都是存在return里面的。

Long URLs, often cluttered with keywords and tracking parameters, can deter visitors. A URL shortening script offers a solution, creating concise links ideal for social media and other platforms. These scripts are valuable for individual websites a

Following its high-profile acquisition by Facebook in 2012, Instagram adopted two sets of APIs for third-party use. These are the Instagram Graph API and the Instagram Basic Display API.As a developer building an app that requires information from a

Laravel simplifies handling temporary session data using its intuitive flash methods. This is perfect for displaying brief messages, alerts, or notifications within your application. Data persists only for the subsequent request by default: $request-

This is the second and final part of the series on building a React application with a Laravel back-end. In the first part of the series, we created a RESTful API using Laravel for a basic product-listing application. In this tutorial, we will be dev

Laravel provides concise HTTP response simulation syntax, simplifying HTTP interaction testing. This approach significantly reduces code redundancy while making your test simulation more intuitive. The basic implementation provides a variety of response type shortcuts: use Illuminate\Support\Facades\Http; Http::fake([ 'google.com' => 'Hello World', 'github.com' => ['foo' => 'bar'], 'forge.laravel.com' =>

The PHP Client URL (cURL) extension is a powerful tool for developers, enabling seamless interaction with remote servers and REST APIs. By leveraging libcurl, a well-respected multi-protocol file transfer library, PHP cURL facilitates efficient execution of various network protocols, including HTTP, HTTPS, and FTP. This extension offers granular control over HTTP requests, supports multiple concurrent operations, and provides built-in security features.

Do you want to provide real-time, instant solutions to your customers' most pressing problems? Live chat lets you have real-time conversations with customers and resolve their problems instantly. It allows you to provide faster service to your custom

The 2025 PHP Landscape Survey investigates current PHP development trends. It explores framework usage, deployment methods, and challenges, aiming to provide insights for developers and businesses. The survey anticipates growth in modern PHP versio


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

WebStorm Mac version
Useful JavaScript development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

SublimeText3 Chinese version
Chinese version, very easy to use

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

Dreamweaver Mac version
Visual web development tools
