PHP 删除未知子目录中的图片
PHP delete images in unknown subdirectories
我正在寻找一种方法来删除随机命名的文件夹中超过 30 天的图像。
我的服务器上有以下目录结构:
mainDirectory (folder)
imagedeletescript.php (script)
images (folder)
uploads (folder)
randomNamedFolder (folder)
randomNamedFolder (folder)
randomNamedFolder (folder)
randomNamedFolder (folder)
etc.
这是我的 imagedeletescript.php:
<?
$days = 30;
$dir = dirname ("/images/uploads");
$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";
}
?>
上面的脚本将删除上传文件夹中超过 30 天的 randomNamedFolders,这不是我想要的。
我怎样才能让脚本扫描上传文件夹中所有随机命名的文件夹,并删除随机命名文件夹中超过 30 天的所有图像?
的组合
$days = 30;
$images = glob('/images/uploads/{*.png,*.jpg,*.bmp}', GLOB_BRACE);
foreach ($images as $image) {
$stats = stat($image);
if ($stats[9] < (time() - (86400 * $days)) {
unlink($image);
}
}
这会在文件夹 /images/uploads
中查找扩展名为 .png
、.jpg
或 .bmp
的文件(无论其深度如何)并检查它们是否超过 30 天。
提示: 虽然与您的问题没有直接关系:正如@D4V1D 所指出的,即使只有这种情况下的一种情况。
您必须在主 loop
中复制 while
循环,或者您可以这样使用 scandir()
或 glob()
:
(...)
while (( $file = readdir($handle)) !== false ) {
if ( $file == '.' || $file == '..' || is_dir($dir.'/'.$file) ) {
continue;
}
$curDir = "$dir/$file";
foreach( scandir( $file ) as $rndFile ) {
if ( $rndFile == '.' || $rndFile == '..' || is_dir("$curDir/$rndFile") ) continue;
if ((time() - filemtime("$curDir/$rndFile")) > ($days *86400)) {
$nofiles++;
unlink($dir.'/'.$file);
}
}
}
(...)
最好的解决方案是实现递归。您可以扫描所有目录和子目录,甚至更深的目录。
<?php
$days = 30,$deleted = 0;
function delete_old_files($dir) {
global $days,$deleted;
if(!is_dir($dir)){
return;
}
$files = preg_grep('/^([^.])/', scandir($dir));
foreach($files as $file) {
$path = $dir.'/'.$file;
if(is_dir($path)){
//the current file is a directory, re-scan it
delete_old_files($path);
continue;
}
if(time() - filemtime($path) > $days * 86400){
unlink($file) ? ++$deleted : null;
}
}
return $deleted;
}
//now call this function
delete_old_files("/images/uploads");
我正在寻找一种方法来删除随机命名的文件夹中超过 30 天的图像。
我的服务器上有以下目录结构:
mainDirectory (folder)
imagedeletescript.php (script)
images (folder)
uploads (folder)
randomNamedFolder (folder)
randomNamedFolder (folder)
randomNamedFolder (folder)
randomNamedFolder (folder)
etc.
这是我的 imagedeletescript.php:
<?
$days = 30;
$dir = dirname ("/images/uploads");
$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";
}
?>
上面的脚本将删除上传文件夹中超过 30 天的 randomNamedFolders,这不是我想要的。
我怎样才能让脚本扫描上传文件夹中所有随机命名的文件夹,并删除随机命名文件夹中超过 30 天的所有图像?
$days = 30;
$images = glob('/images/uploads/{*.png,*.jpg,*.bmp}', GLOB_BRACE);
foreach ($images as $image) {
$stats = stat($image);
if ($stats[9] < (time() - (86400 * $days)) {
unlink($image);
}
}
这会在文件夹 /images/uploads
中查找扩展名为 .png
、.jpg
或 .bmp
的文件(无论其深度如何)并检查它们是否超过 30 天。
提示: 虽然与您的问题没有直接关系:正如@D4V1D 所指出的,即使只有这种情况下的一种情况。
您必须在主 loop
中复制 while
循环,或者您可以这样使用 scandir()
或 glob()
:
(...)
while (( $file = readdir($handle)) !== false ) {
if ( $file == '.' || $file == '..' || is_dir($dir.'/'.$file) ) {
continue;
}
$curDir = "$dir/$file";
foreach( scandir( $file ) as $rndFile ) {
if ( $rndFile == '.' || $rndFile == '..' || is_dir("$curDir/$rndFile") ) continue;
if ((time() - filemtime("$curDir/$rndFile")) > ($days *86400)) {
$nofiles++;
unlink($dir.'/'.$file);
}
}
}
(...)
最好的解决方案是实现递归。您可以扫描所有目录和子目录,甚至更深的目录。
<?php
$days = 30,$deleted = 0;
function delete_old_files($dir) {
global $days,$deleted;
if(!is_dir($dir)){
return;
}
$files = preg_grep('/^([^.])/', scandir($dir));
foreach($files as $file) {
$path = $dir.'/'.$file;
if(is_dir($path)){
//the current file is a directory, re-scan it
delete_old_files($path);
continue;
}
if(time() - filemtime($path) > $days * 86400){
unlink($file) ? ++$deleted : null;
}
}
return $deleted;
}
//now call this function
delete_old_files("/images/uploads");