可能重复:
glob() 有否定吗?
我想从目录中删除所有文件(可以是任意数量的文件扩展名),除了其中的单个 index.html 。
我在用着:
$path = "/assets/cache/";
foreach(glob($path ."*.*") as $file) {
unlink($file);
}
但不能为我的生活怎么说取消链接,如果不是.html!
谢谢!
可能重复:
glob() 有否定吗?
我想从目录中删除所有文件(可以是任意数量的文件扩展名),除了其中的单个 index.html 。
我在用着:
$path = "/assets/cache/";
foreach(glob($path ."*.*") as $file) {
unlink($file);
}
但不能为我的生活怎么说取消链接,如果不是.html!
谢谢!
尝试
$path = "/assets/cache/";
foreach(glob($path ."*.*") as $file) {
if(pathinfo($file, PATHINFO_EXTENSION) != 'html') {
unlink($file);
}
}
如果您还想删除其他 html 文件(“index.html”除外):
$path = "/assets/cache/";
foreach(glob($path ."*.*") as $file) {
if(pathinfo($file, PATHINFO_BASENAME) != 'index.html') {
unlink($file);
}
}
在这里试试这个...
$path = "/assets/cache/";
foreach(glob($path ."*.*") as $file) {
$pathPart = explode(".",$file);
$fileEx = $pathPart[count($pathPart)-1];
if($fileEx != "html" && $fileEx != "htm"){
unlink($file);
}
}
php 函数glob
没有否定,但是 PHP 可以通过以下方式为您提供两个 glob 之间的区别array_diff
:
$all = glob("*.*");
$not = glob("php_errors.log");
var_dump(
$all,
$not,
array_diff($all, $not)
);
查看演示:http ://codepad.org/RBFwPUWm
如果您不想使用数组,我强烈建议您查看 PHPs 目录迭代器。