简体   繁体   中英

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. 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.

You could also just use sudo chmod -R to recursively apply chmod to the files in a directory and its sub-directories.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM