递归 chmod/chown/chgrp 目录中的所有文件和文件夹
我正在一个构建其他网站的网站上工作。有些时候我使用 copy() 创建文件和目录,其他时候我正在 php 中构建 XML 文件并使用 DOMDocument::save 保存它们。最终结果是一个具有各种混乱权限的根文件夹。我一直在修改文件和文件夹,这在某种程度上是这样,但在使用 copy()
时我尤其遇到麻烦。
(这是我到目前为止的位置http://pastebin.com/SBE8vtFX,attn:function modPath($path)
)
我想采取不同的方法,并递归地 chmod/chown/chgrp 我的文档根目录中的所有文件和文件夹符合我的规范一次。
以文档根目录
/home/mysite/public_html
和 public_html
为例,我有
-rwxrwxrwx 1 mysite mysite 348 Aug 31 10:49 index.php
d--------x 5 root root 4096 Aug 30 10:21 folder1
drwxrwxrwx 2 mysite mysite 4096 Aug 30 09:41 folder2
一个问题:
如何同时修改指定目录中的所有文件?我还想区分目录和文件夹之间的不同 chmod 设置。这需要一个 PHP 解决方案。
据我所知
<?php
function modAll($root) {
$aPath = explode("/", $root);
$user = $aPath[2];
/* Some sort of looping through $root */ {
$mod = (is_dir($thisfileorfolder) ? 0755 : 0644);
chmod($thisfileorfolder, $mod);
chown($thisfileorfolder, $user);
chgrp($thisfileorfolder, $user);
}
}
?>
I am working on a site which builds other sites. Some if it I use copy() to create the files and directories, other times I'm building XML files in php and using DOMDocument::save to save them. The end result is a root folder with all sorts of messed up permissions. I've beening modding files and folders as I go, which words to some extent, but I'm particularly having trouble when it comes to using copy()
.
(This is where I'm at so far http://pastebin.com/SBE8vtFX, attn: function modPath($path)
)
I want to take a different approach and recursively chmod/chown/chgrp all the files and folders within my document root to my specifications at once.
Take for example the document root
/home/mysite/public_html
and within public_html
I have
-rwxrwxrwx 1 mysite mysite 348 Aug 31 10:49 index.php
d--------x 5 root root 4096 Aug 30 10:21 folder1
drwxrwxrwx 2 mysite mysite 4096 Aug 30 09:41 folder2
My question:
How can I mod all files within a specified directory at once? I want to differentiate different chmod settings between directories and folders as well. This needs to be a PHP solution.
This is as far as I can get
<?php
function modAll($root) {
$aPath = explode("/", $root);
$user = $aPath[2];
/* Some sort of looping through $root */ {
$mod = (is_dir($thisfileorfolder) ? 0755 : 0644);
chmod($thisfileorfolder, $mod);
chown($thisfileorfolder, $user);
chgrp($thisfileorfolder, $user);
}
}
?>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这应该会有帮助。
编辑:纠正了一些语法错误
This should be helpful.
EDITED: some syntax errors corrected
您可以执行 系统 调用,
当然您使用 escapeshellarg() 或 escapeshellcmd( )
以避免执行任意命令
You can perform a system call
of course you use escapeshellarg() or escapeshellcmd()
in order to avoid executing arbitrary commands
模式 493 无效意味着您以十进制形式传递了模式。先转换为八进制字符串。
Invalid mode 493 means you passed your mode as decimal. Convert to octal string first.