就是现在我有一个存放日志的文件夹,需要怎么做才能用程序做到将较早的日志文件进行删除?大神们有方法么?
回复讨论(解决方案)
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里面的。

TooptimizePHPcodeforreducedmemoryusageandexecutiontime,followthesesteps:1)Usereferencesinsteadofcopyinglargedatastructurestoreducememoryconsumption.2)LeveragePHP'sbuilt-infunctionslikearray_mapforfasterexecution.3)Implementcachingmechanisms,suchasAPC

PHPisusedforsendingemailsduetoitsintegrationwithservermailservicesandexternalSMTPproviders,automatingnotificationsandmarketingcampaigns.1)SetupyourPHPenvironmentwithawebserverandPHP,ensuringthemailfunctionisenabled.2)UseabasicscriptwithPHP'smailfunct

The best way to send emails is to use the PHPMailer library. 1) Using the mail() function is simple but unreliable, which may cause emails to enter spam or cannot be delivered. 2) PHPMailer provides better control and reliability, and supports HTML mail, attachments and SMTP authentication. 3) Make sure SMTP settings are configured correctly and encryption (such as STARTTLS or SSL/TLS) is used to enhance security. 4) For large amounts of emails, consider using a mail queue system to optimize performance.

CustomheadersandadvancedfeaturesinPHPemailenhancefunctionalityandreliability.1)Customheadersaddmetadatafortrackingandcategorization.2)HTMLemailsallowformattingandinteractivity.3)AttachmentscanbesentusinglibrarieslikePHPMailer.4)SMTPauthenticationimpr

Sending mail using PHP and SMTP can be achieved through the PHPMailer library. 1) Install and configure PHPMailer, 2) Set SMTP server details, 3) Define the email content, 4) Send emails and handle errors. Use this method to ensure the reliability and security of emails.

ThebestapproachforsendingemailsinPHPisusingthePHPMailerlibraryduetoitsreliability,featurerichness,andeaseofuse.PHPMailersupportsSMTP,providesdetailederrorhandling,allowssendingHTMLandplaintextemails,supportsattachments,andenhancessecurity.Foroptimalu

The reason for using Dependency Injection (DI) is that it promotes loose coupling, testability, and maintainability of the code. 1) Use constructor to inject dependencies, 2) Avoid using service locators, 3) Use dependency injection containers to manage dependencies, 4) Improve testability through injecting dependencies, 5) Avoid over-injection dependencies, 6) Consider the impact of DI on performance.

PHPperformancetuningiscrucialbecauseitenhancesspeedandefficiency,whicharevitalforwebapplications.1)CachingwithAPCureducesdatabaseloadandimprovesresponsetimes.2)Optimizingdatabasequeriesbyselectingnecessarycolumnsandusingindexingspeedsupdataretrieval.


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

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

Zend Studio 13.0.1
Powerful PHP integrated development environment

WebStorm Mac version
Useful JavaScript development tools

SublimeText3 Chinese version
Chinese version, very easy to use
