如何在 x 时间后(比如说 24 小时后)自动删除子目录下的所有文件 - 而不使用来自服务器或 pl 的 cronjob 命令。您如何仅使用 PHP 代码或仅访问页面而不单击某些内容并自动运行命令来做到这一点。
8 回答
对我的第一个答案的最后评论的回应。我要编写代码示例,所以我创建了另一个答案,而不是再添加一个评论。
要删除具有自定义扩展名的文件,您必须实现代码:
<?php
$path = dirname(__FILE__).'/files';
if ($handle = opendir($path)) {
while (false !== ($file = readdir($handle))) {
if ((time()-filectime($path.'/'.$file)) < 86400) { // 86400 = 60*60*24
if (preg_match('/\.txt$/i', $file)) {
unlink($path.'/'.$file);
}
}
}
}
?>
评论: 1. 这个例子使用了正则表达式/\.txt$/i
,这意味着只有带有扩展名的文件txt
才会被删除。'$' 符号意味着,该文件名必须以字符串 '.txt' 结尾。标志“i”表示,该比较将不区分大小写。更多关于preg_match()函数。
此外,您可以使用strripos()函数搜索具有特定扩展名的文件。这是代码片段:
<?php
$path = dirname(__FILE__).'/files';
if ($handle = opendir($path)) {
while (false !== ($file = readdir($handle))) {
if ((time()-filectime($path.'/'.$file)) < 86400) { // 86400 = 60*60*24
if (strripos($file, '.txt') !== false) {
unlink($path.'/'.$file);
}
}
}
}
?>
评论:这个例子似乎更明显。的结果strripos()
也可以通过组合两个函数来实现:strrpos(strtolower($file), '.txt')
但是,恕我直言,在代码中使用更少的函数以使其更具可读性和更小是一个很好的规则。请仔细阅读 strripos() 函数(返回值块)页面上的警告。
一个更重要的注意事项:如果您使用的是 UNIX 系统,文件删除可能会因为文件权限而失败。您可以查看有关chmod()功能的手册。
祝你好运。
您可以使用 PHP 核心函数filectime()和unlink()来检查文件创建时间并删除其文件/文件。
编辑。 代码示例:
if ($handle = opendir('/path/to/files')) {
while (false !== ($file = readdir($handle))) {
if (filectime($file)< (time()-86400)) { // 86400 = 60*60*24
unlink($file);
}
}
}
好吧,我们开始,删除 X 天前的文件的 PHP 脚本。
<?
$days = 1;
$dir = dirname ( __FILE__ );
$nofiles = 0;
if ($handle = opendir($dir)) {
while (( $file = readdir($handle)) !== false ) {
if ( $file == '.' || $file == '..' || is_dir($dir.'/'.$file) ) {
continue;
}
if ((time() - filemtime($dir.'/'.$file)) > ($days *86400)) {
$nofiles++;
unlink($dir.'/'.$file);
}
}
closedir($handle);
echo "Total files deleted: $nofiles \n";
}
?>
现在粘贴此代码并将其保存为 php 文件,将其上传到要删除文件的文件夹。你可以在这段 php 代码的开头看到
$days = 1;
设置天数,例如,如果您将其设置为 2,那么超过 2 天的文件将被删除。基本上,当您运行脚本、获取当前目录并读取文件条目、跳过“。”时,就会发生这种情况。对于当前目录并进一步检查是否有任何其他目录,
if ( $file == '.' || $file == '..' || is_dir($dir.'/'.$file) ) {
continue;
}
如果文件条目不是目录,那么它会获取文件修改时间(最后修改时间)并比较,如果它是天数
if ((time() - filemtime($dir.'/'.$file)) > ($days *86400)) {
$nofiles++;
unlink($dir.'/'.$file);
}
如果条件为真,那么它将在 unlink() php 函数的帮助下删除文件。最后关闭目录并退出。我还添加了一个计数器来计算正在删除的文件数,该计数器将在删除过程结束时显示。所以把php文件放在需要删除文件的目录下执行。
希望这会有所帮助:)
我使用 shell AT 命令,虽然它就像一个 cronjob
php:
exec("echo rm /somedir/somefile.ext|at now +24 hours");
这是另一个使用 GLOB 的示例,它将删除任何文件
$files = glob('path/to/your/files/*');
foreach($files as $file) { // iterate files
// if file creation time is more than 5 minutes
if ((time() - filectime($file)) > 3600) { // 86400 = 60*60*24
unlink($file);
}
}
或者如果您想排除某些文件
$files = preg_grep('#\.txt#', glob('/path/to/your/files/*'), PREG_GREP_INVERT);
在尝试使用这些示例后,我遇到了几个问题:
- 比较运算符应大于,不小于
- filemtime 返回修改后的时间。filectime 是文件的 inode 数据更改的时间;即当inode中的权限、所有者、组或其他元数据更新时,可能会导致意外结果
我将示例更改为使用 filemtime 并将比较修复如下:
<?php
$path = dirname(__FILE__).'/files';
if ($handle = opendir($path)) {
while (false !== ($file = readdir($handle))) {
if ((time()-filemtime($path.'/'.$file)) > 86400) { // 86400 = 60*60*24
if (preg_match('/\.txt$/i', $file)) {
unlink($path.'/'.$file);
}
}
}
}
?>
function deleteCachedData($hours=24)
{
$files = glob(ROOTDIR.DIRECTORY_SEPARATOR.'tmp'.DIRECTORY_SEPARATOR.'*.txt');
foreach($files as $file) {
if(is_file($file) && (time() - filectime($file)) > $hours*3600) {
unlink($file);
}
}
}
$path = 'folder/subfolder/';
/* foreach (glob($path.'.txt') as $file) { */
foreach (glob($path.'*') as $file) {
if(time() - filectime($file) > 86400){
unlink($file);
}
}