简体   繁体   English

使用RecursiveIteratorIterator递归chmod文件

[英]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. 我正在尝试使用RecursiveDirectoryIteratorRecursiveIteratorIterator 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.

相关问题 乘法foreach递归或RecursiveIteratorIterator - Multiples foreach recursively or RecursiveIteratorIterator 使用RecursiveIteratorIterator显示所有子文件和地图 - Using the RecursiveIteratorIterator to show all the child files and maps AJAX和PHP根据需要使用RecursiveIteratorIterator获取文件和文件夹列表 - AJAX and PHP get files and folders list using RecursiveIteratorIterator as needed 递归 chmod/chown/chgrp 目录中的所有文件和文件夹 - Recursively chmod/chown/chgrp all files and folder within a directory 将RecursiveIteratorIterator与命名空间一起使用? - Using RecursiveIteratorIterator with namespaces? 文件到数组-Glob vs RecursiveIteratorIterator - Files to array - glob vs RecursiveIteratorIterator 如何使用RecursiveIteratorIterator忽略目录? - How to ignore directories using RecursiveIteratorIterator? 使用RecursiveIteratorIterator列出文件夹中的文件很棒。 我也可以获得父路径和完整路径吗? - Using RecursiveIteratorIterator to list files in folder is great. Can I get the parent path and full path as well? RecursiveIteratorIterator:在zip中包含目录和php文件 - RecursiveIteratorIterator: include directory and php files in zip 为什么我不能在PHP中使用chmod()上传文件? - Why I can not upload files using chmod() in PHP?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM