[英]Recursively chmod/chown/chgrp all files and folder within a directory
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.
有些时候我使用copy()创建文件和目录,其他时候我在 php 中构建 XML 文件并使用DOMDocument::save保存它们。 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()
.我一直在修改文件和文件夹,因为我是 go,这在某种程度上是这样的,但是在使用
copy()
时我特别遇到了麻烦。
(This is where I'm at so far http://pastebin.com/SBE8vtFX , attn: function modPath($path)
) (到目前为止,这是我所在的位置http://pastebin.com/SBE8vtFX ,收件人:
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 .我想采用不同的方法,并递归地一次将我的文档根目录中的所有文件和文件夹chmod/chown/chgrp更改为我的规范。
Take for example the document root以文档根目录为例
/home/mysite/public_html
and within public_html
I have在
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
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.
我也想区分目录和文件夹之间的不同 chmod 设置。 This needs to be a PHP solution.
这需要一个 PHP 解决方案。
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);
}
}
?>
This should be helpful.这应该会有所帮助。 EDITED: some syntax errors corrected
已编辑:更正了一些语法错误
function fsmodify($obj) {
$chunks = explode('/', $obj);
chmod($obj, is_dir($obj) ? 0755 : 0644);
chown($obj, $chunks[2]);
chgrp($obj, $chunks[2]);
}
function fsmodifyr($dir)
{
if($objs = glob($dir."/*")) {
foreach($objs as $obj) {
fsmodify($obj);
if(is_dir($obj)) fsmodifyr($obj);
}
}
return fsmodify($dir);
}
system("/bin/chmod -R $mod $root");
system("/usr/bin/find -type d $root -print0 | xargs -0 | /bin/chmod $moddir");
system("/bin/chown -R $user $root");
system("/bin/chgrp -R $user $root");
Invalid mode 493 means you passed your mode as decimal.无效模式 493 表示您以十进制形式传递了您的模式。 Convert to octal string first.
首先转换为八进制字符串。
I have coded a library for that.我为此编写了一个库。
Example of usage:使用示例:
<?php
use MathiasReker\FilePerm;
require __DIR__ . '/vendor/autoload.php';
(new FilePerm([__DIR__])) // <-- root directory
->setDefaultModeFile(0644) // <-- file permission
->setDefaultModeFolder(0755) // <-- folder permission
->scan()
->fix();
Full documentation: https://github.com/MathiasReker/php-file-permissions完整文档: https://github.com/MathiasReker/php-file-permissions
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.