问题描述
好像
chmod("*.txt",0660);
不起作用。
我知道我可以:
- chmod 一个一个的文件:它可以工作,但我事先不知道所有名称,所以我无法使用它。
- 使用
exec
。它有效,但我不喜欢它,原因有几个(速度、安全性等)。 - 使用
scandir
。它可以工作,但又很慢,我想对于一个简单的操作来说太多了
我真的很想直接使用chmod。甚至有可能吗?谢谢。
解决方法
所以你可以像你提到的那样使用 scandir
来做到这一点,是的,文件系统可能会很慢,你可以添加一个签入,这样你就不会对已经处理过的文件这样做
<?php
$files = scandir('./');
foreach ($files as $file) {
// check here so you don't have to do every file again
if (substr(sprintf('%o',fileperms($file)),-4) === "0660") {
echo "skipping " . $file;
continue;
}
$extension = pathinfo($file)['extension'];
if ($extension === 'txt') {
chmod($file,0660);
}
}
或者你可以使用 glob
<?php
$files = glob('./*.{txt}',GLOB_BRACE);
foreach($files as $file) {
// check here so you don't have to do every file again
if (substr(sprintf('%o',0660);
}
}