PHP删除过期目录中的所有子目录?
有一个目录/home/example/public_html/users/files/
。该目录中有一些具有随机名称的子目录,例如 2378232828923_1298295497
。
如何彻底删除创建日期>的子目录1个月?
我用一个很好的脚本来删除文件,但它不适用于目录:
$seconds_old = 2629743; //1 month old
$directory = "/home/example/public_html/users/files/";
if( !$dirhandle = @opendir($directory) )
return;
while( false !== ($filename = readdir($dirhandle)) ) {
if( $filename != "." && $filename != ".." ) {
$filename = $directory. "/". $filename;
if( @filectime($filename) < (time()-$seconds_old) )
@unlink($filename); //rmdir maybe?
}
}
There is a directory /home/example/public_html/users/files/
. Within the directory there are subdirectories with random names like 2378232828923_1298295497
.
How do I completely delete the subdirectories which have creation date > 1 month?
There is a good script that I use to delete files, but it don't work with dirs:
$seconds_old = 2629743; //1 month old
$directory = "/home/example/public_html/users/files/";
if( !$dirhandle = @opendir($directory) )
return;
while( false !== ($filename = readdir($dirhandle)) ) {
if( $filename != "." && $filename != ".." ) {
$filename = $directory. "/". $filename;
if( @filectime($filename) < (time()-$seconds_old) )
@unlink($filename); //rmdir maybe?
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
为此你需要一个递归函数。
you need a recursive function for this.
如果 PHP 在 Linux 服务器上运行,您可以使用 shell 命令来提高性能(递归 PHP 函数在非常大的目录中可能效率低下):
shell_exec('rm -rf '.$directory);
If PHP runs on a Linux server, you could use a shell command, to improve performance (a recursive PHP function can be inefficient in very large directories):
shell_exec('rm -rf '.$directory);