2012-11-07 49 views
1

可能重复删除所有,但一个文件类型的目录:
Does glob() have negation?使用水珠PHP

我想从目录中删除所有文件(可以是任何数量的文件扩展的)开从那里的单个index.html。

我使用:

$path = "/assets/cache/"; 

foreach(glob($path ."*.*") as $file) { 
    unlink($file); 
} 

但不能为我的生命怎么说取消链接,如果不是.HTML!

谢谢!

回答

4

试试这个这里...

$path = "/assets/cache/"; 

foreach(glob($path ."*.*") as $file) { 
    $pathPart = explode(".",$file); 
    $fileEx = $pathPart[count($pathPart)-1]; 
    if($fileEx != "html" && $fileEx != "htm"){ 
     unlink($file); 
    } 
} 
+0

你先生,是一个bloomin天才。完善!! – bjohnb

+0

高兴地帮助我的朋友。如果你得到了你需要的答案,不要忘记接受(:谢谢。 – George

+1

当时我不能接受,因为它说我必须再等4分钟!Gahh!我现在才回来接受它!我不会忘记你的帮助:) – bjohnb

4

尝试

$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); 
    } 
} 
1

php函数glob没有否定,但是PHP可以通过给你两个小球之间的区别3210:

$all = glob("*.*"); 
$not = glob("php_errors.log"); 

var_dump(
    $all, 
    $not, 
    array_diff($all, $not) 
); 

观看演示:http://codepad.org/RBFwPUWm

如果你不想使用数组,我强烈建议去看看到PHP的目录迭代器。