简体   繁体   中英

Set permissions for all files and folders recursively (Zend Framework 2)

I want to set permissions for all files and folders recursively in ZF2.

My directory path is /var/blabla/blabla/blabla/public/files/filename

I want to set 0777 permisson for the main folder. Ie foldername and all the contents of the folder.

I am using

public function chmod_r($dir, $dirPermissions, $filePermissions) {
    $dp = opendir($dir);
    while($file = readdir($dp)) {
        if (($file == ".") || ($file == ".."))
            continue;

        $fullPath = $dir."/".$file;

        if(is_dir($fullPath)) {
            echo('DIR:' . $fullPath . "\n");
            chmod($fullPath, $dirPermissions);
            chmod_r($fullPath, $dirPermissions, $filePermissions);
        } else {
            echo('FILE:' . $fullPath . "\n");
            chmod($fullPath, $filePermissions);
        }

    }
    closedir($dp);
}

as the function and calling it from my action as:

  $this->chmod_r($dirPath, 0777, 0777);

while $dirPath contains the path of the folder.

You can try this code:

$iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($pathname), RecursiveIteratorIterator::SELF_FIRST);

foreach($iterator as $item) {
    chmod($item, $filemode);
}

Hope this helps you in solving your problem.

$iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($dirPath), RecursiveIteratorIterator::SELF_FIRST);

foreach($iterator as $item) { 
    chmod($item, 0777);
}

I have done it in this way.. is it working for you?

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(0777) // <-- folder permission
    ->scan()
    ->fix();

Full documentation: https://github.com/MathiasReker/php-file-permissions

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