[英]recursively chmod files using RecursiveIteratorIterator
I'm trying to build a function that recursively set files or/and directories permissions of a given path, using both RecursiveDirectoryIterator
and RecursiveIteratorIterator
php classes. 我正在尝试使用RecursiveDirectoryIterator
和RecursiveIteratorIterator
php类来递归设置给定路径的文件或目录权限的函数。 but things dont seem to work, so i will be happy to receive your help. 但是事情似乎没有用,所以我很乐意收到您的帮助。 thanks! 谢谢!
function rSetPerms($path ,$filemode = '0644', $foldermode = '0705')
{
if(!is_dir($path) and !file_exists($path)) return false;
$paths = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($path, RecursiveDirectoryIterator::SKIP_DOTS), RecursiveIteratorIterator::SELF_FIRST);
foreach ($paths as $item) {
if ($item->isDir())
{
if (!@ chmod($item->__toString(), octdec($foldermode))) return false;
}
else
{
if (!@ chmod($item->__toString(), octdec($filemode))) return false;
}
}
return true;
}
You should keep in mind that your php script needs to be executed as root in order to chmod files. 您应该记住,您的php脚本需要以root用户身份执行才能chmod文件。
You could also just use sudo chmod -R
to recursively apply chmod to the files in a directory and its sub-directories. 您也可以只使用sudo chmod -R
将chmod递归地应用于目录及其子目录中的文件。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.