如何在 PHP 中为文件夹及其所有子文件夹和文件设置 chmod? [复制]

Posted

技术标签:

【中文标题】如何在 PHP 中为文件夹及其所有子文件夹和文件设置 chmod? [复制]【英文标题】:How to set chmod for a folder and all of its subfolders and files in PHP? [duplicate] 【发布时间】:2017-01-15 02:22:15 【问题描述】:

我认为我的问题说明了一切。我想使用 phpchmod() 函数 对文件夹、其子文件夹和文件设置 777 权限

任何帮助表示赞赏。谢了。

【问题讨论】:

【参考方案1】:
chmod("Folder",0770);

php 中的函数允许您更改文件的权限和 对于递归更改,请使用 exec

exec ("find /path/to/folder -type d -exec chmod 0770  +");//for sub directory
exec ("find /path/to/folder -type f -exec chmod 0644  +");//for files inside directory

确保您的网络服务器对该文件夹具有写入权限。

查看这些以获取更多详细信息http://php.net/manual/en/function.chmod.phphttp://www.w3schools.com/php/func_filesystem_chmod.asp

【讨论】:

【参考方案2】:

在 php 中有很长的方法可以做到这一点,我个人会通过 PHP 可以与之交互的命令行来完成。

在命令行 (Linux/Unix) 上,您可以执行 chmod options permissions filename

要递归更改权限,您可以使用chmod -R 0777 masterFile

所以在 PHP 中你会这样做 exec("chmod -R 0777 masterFile");

-R 表示递归,因此它会转到您的子文件夹

单独使用 PHP 的冗长方法是获取子文件夹数组并执行 foreach 循环并在 PHP 中运行 chmod() 函数,但这种方式更简洁。

有关 linux/unix chmod 的更多信息,请参阅this link

希望这会有所帮助。

【讨论】:

以上是关于如何在 PHP 中为文件夹及其所有子文件夹和文件设置 chmod? [复制]的主要内容,如果未能解决你的问题,请参考以下文章