filemtime() 在 Windows PHP-CLI 中不起作用
我刚刚让 PHP-CLI 在我的 Windows 机器上运行,这样我就可以使用 PHP 创建脚本。 但是,我正在尝试编写一个脚本来清理 Firefox 下载文件夹中早于 X 天的文件,但我似乎无法使 filemtime() 函数正常工作。
这是我编写的函数:
function deleteOldFiles($dir, $days) {
$mydir = opendir($dir);
while(($file = readdir($mydir)) !== false) {
if($file != "." && $file != ".." && (filemtime($dir.$file) <= time() - ($days * 86400))) {
//unlink($dir.$file) or DIE("Failed to delete $dir$file<br />");
echo filemtime($dir.$file);
}
}
closedir($mydir);
}
运行此函数后,每个文件都会出现以下错误:
Warning: filemtime(): stat failed for E:\My Documents\Downloadsphp-5.3.0-nts-Win32-VC9-x86.msi in E:\_scripts\cleanupDownloads\cleanupDownloads.php on line 10
根据我所做的研究,filemtime() 应该在 Windows 中工作。 我究竟做错了什么?
I just got PHP-CLI working on my Windows machine so I could create scripts using PHP. However, I'm trying to write a script to cleanup my Firefox downloads folder of files older than X number of days, but I can't seem to get the filemtime() function working.
Here is the function I wrote:
function deleteOldFiles($dir, $days) {
$mydir = opendir($dir);
while(($file = readdir($mydir)) !== false) {
if($file != "." && $file != ".." && (filemtime($dir.$file) <= time() - ($days * 86400))) {
//unlink($dir.$file) or DIE("Failed to delete $dir$file<br />");
echo filemtime($dir.$file);
}
}
closedir($mydir);
}
And upon running this I get the following error for every file:
Warning: filemtime(): stat failed for E:\My Documents\Downloadsphp-5.3.0-nts-Win32-VC9-x86.msi in E:\_scripts\cleanupDownloads\cleanupDownloads.php on line 10
From the research I've done, filemtime() should work in Windows. What am I doing wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
警告:filemtime():第 10 行 E:\_scripts\cleanupDownloads\cleanupDownloads.php 中的 E:\My Documents\Downloadsphp-5.3.0-nts-Win32-VC9-x86.msi 统计失败
您在目录和文件名之间缺少反斜杠
\
。 不要忘记在 Windows 上对其进行转义 (\\
),或者仅使用正斜杠 (/
) 或 PHP 的DIRECTORY_SEPARATOR
常量。编辑
并查看scandir(),glob() 或 目录迭代器。
Warning: filemtime(): stat failed for E:\My Documents\Downloadsphp-5.3.0-nts-Win32-VC9-x86.msi in E:\_scripts\cleanupDownloads\cleanupDownloads.php on line 10
You're missing a backslash
\
between the directory and the filename. Don't forget to escape it on Windows (\\
) or just use the forward slash (/
) or PHP'sDIRECTORY_SEPARATOR
constant.edit
And have a look at scandir(), glob() or DirectoryIterator.