Php filemtime“;“的警告统计失败”;
我已经读了很多关于它的问题和答案,但我仍然不能解决我的问题 我试图创建一个函数,删除一天前创建的所有扩展名为“xml”或“xsl”的文件。但我在我的每个文件上都得到了这样的警告: 警告:filemtime()[function.filemtime]:第44行/home/u188867248/public_html/ampc/library.php中的post_1003463425.xml的stat失败 此目录的所有文件都具有相同的结构名称“post_+randomNum+.xml”(例如:post_1003463425.xml或post_1456463425.xsl)。所以我认为这不是编码问题(就像我在其他问题中看到的那样) 我的函数代码如下:Php filemtime“;“的警告统计失败”;,php,regex,filemtime,Php,Regex,Filemtime,我已经读了很多关于它的问题和答案,但我仍然不能解决我的问题 我试图创建一个函数,删除一天前创建的所有扩展名为“xml”或“xsl”的文件。但我在我的每个文件上都得到了这样的警告: 警告:filemtime()[function.filemtime]:第44行/home/u188867248/public_html/ampc/library.php中的post_1003463425.xml的stat失败 此目录的所有文件都具有相同的结构名称“post_+randomNum+.xml”(例如:pos
function deleteOldFiles(){
if ($handle = opendir('./xml')) {
while (false !== ($file = readdir($handle))) {
if(preg_match("/^.*\.(xml|xsl)$/i", $file)){
$filelastmodified = filemtime($file);
if ( (time()-$filelastmodified ) > 24*3600){
unlink($file);
}
}
}
closedir($handle);
}
}
感谢您的帮助:)我认为问题在于文件的真实路径。例如,您的脚本正在处理“/”,您的文件位于目录“/”中。因此,在获取filemtime或取消链接之前,最好检查该文件是否存在:
function deleteOldFiles(){
if ($handle = opendir('./xml')) {
while (false !== ($file = readdir($handle))) {
if(preg_match("/^.*\.(xml|xsl)$/i", $file)){
$fpath = 'xml/'.$file;
if (file_exists($fpath)) {
$filelastmodified = filemtime($fpath);
if ( (time() - $filelastmodified ) > 24*3600){
unlink($fpath);
}
}
}
}
closedir($handle);
}
}
对我来说,所涉及的文件名附加了一个querystring,这是这个函数不喜欢的
$path = 'path/to/my/file.js?v=2'
解决办法是先把它砍掉:
$path = preg_replace('/\?v=[\d]+$/', '', $path);
$fileTime = filemtime($path);
适用于喜欢短代码的用户的较短版本:
// usage: deleteOldFiles("./xml", "xml,xsl", 24 * 3600)
function deleteOldFiles($dir, $patterns = "*", int $timeout = 3600) {
// $dir is directory, $patterns is file types e.g. "txt,xls", $timeout is max age
foreach (glob($dir."/*"."{{$patterns}}",GLOB_BRACE) as $f) {
if (is_writable($f) && filemtime($f) < (time() - $timeout))
unlink($f);
}
}
//用法:deleteOldFiles(“./xml”,“xml,xsl”,24*3600)
函数deleteOldFiles($dir,$patterns=“*”,int$timeout=3600){
//$dir是目录,$patterns是文件类型,例如“txt,xls”,$timeout是最长期限
foreach(glob($dir./*“{{$patterns}}”,glob_BRACE)作为$f){
if(可写($f)和&filemtime($f)<(time()-$timeout))
取消链接(f美元);
}
}
在我的例子中,它与路径或文件名无关。如果filemtime()、fileatime()或filectime()不起作用,请尝试stat()
变成
$stat = stat($directory.$file);
$filedate = date_create(date("Y-m-d", $stat['ctime']));
这对我很管用
按天数删除文件的完整代码段:
$directory = $_SERVER['DOCUMENT_ROOT'].'/directory/';
$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);
if ($days->days > 1)
{
unlink($directory.$file);
}
}
}
@somy-a
$filelastmodified-time()
应该是time()-$filelastmodified
no?@somy-a哈哈,我需要它,在谷歌搜索后发现了它,所以。。。这意味着即使在两年后,你的代码也会引起其他人的兴趣,如果代码是正确的,它可能会更酷!;-)顺便说一下,谢谢你!即使在3年以上的时间里仍然帮助人们:)即使在4年之后!如果有人对我的repo发表了关于filemtime
导致错误的评论(他们忘了构建项目),请将此答案链接到他们,以便他们可以看到这是因为该文件不存在。
$directory = $_SERVER['DOCUMENT_ROOT'].'/directory/';
$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);
if ($days->days > 1)
{
unlink($directory.$file);
}
}
}