【发布时间】:2019-06-28 09:45:12
【问题描述】:
我想通过 cron 任务报告从我在 php 中运行的函数中删除了多少文件。
目前的代码如下:-
<?php
function deleteAll($dir) {
$counter = 0;
foreach(glob($dir . '/*') as $file) {
if(is_dir($file)) {
deleteAll($file); }
else {
if(is_file($file)){
// check if file older than 14 days
if((time() - filemtime($file)) > (60 * 60 * 24 * 14)) {
$counter = $counter + 1;
unlink($file);
}
}
}
}
}
deleteAll("directory_name");
// Write to log file to confirm completed
$fp = fopen("logthis.txt", "a");
fwrite($fp, $counter." files deleted."."\n");
fclose($fp);
?>
这对具有 VBA 背景的我来说很有意义,但我认为在最后写入我的自定义日志文件时,计数器返回 null。我认为共享托管站点在能够全局声明变量或类似变量方面存在一些限制?
感谢任何帮助!如果我不能计算已删除的文件,这不是世界末日,但以我选择的格式记录输出会很好。
【问题讨论】:
-
有函数
return $counter。现在,您正在处理一个范围问题,即在您的fwrite()调用中使用的$counter与您的函数内部不同。 -
只是范围问题。详情请查看this question 和this question。
标签: php function variables return unlink