我有一些脚本可以删除目录中的所有.png文件:
foreach (glob("*.png") as $filename) {
echo "$filename size " . filesize($filename) . "\n";
unlink($filename);
}
如何进一步删除特定文件大小的所有.png
个文件?
我还有一些脚本可以找到目录中所有文件的文件大小:
$bytes = filesize('example.png');
如何将两者合并?
答案 0 :(得分:3)
您可以使用if语句检查大小是否高于阈值:
$threshold = 1024;
foreach (glob("*.png") as $filename) {
$size = filesize($filename);
if ($size > $threshold) {
unlink($filename);
}
}
答案 1 :(得分:1)
$maxsize = 1024; // example
foreach (glob("*.png") as $filename)
{
$filesize = filesize($filename);
echo "$filename size " .$filesize . "\n";
if ($filesize > $maxsize)
unlink($filename);
}
答案 2 :(得分:1)
<?php
$path = '.';
class MyFilter extends FilterIterator {
public function accept() {
$fi = $this->getInnerIterator()->current();
return strlen($fi)-4===strripos($fi, '.png') && 1024 < $fi->getSize();
}
}
foreach (new MyFilter(new DirectoryIterator($path)) as $deleteInfo) {
echo $deleteInfo, "\n";
}
(可能会更灵活......但这只是一个例子)
如果您希望它以递归方式删除,请查看RecursiveDirectoryIterator