Sanjay Tiwari
Sanjay Tiwari

Reputation: 41

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

Upvotes: 1

Views: 719

Answers (2)

Sanjay Tiwari
Sanjay Tiwari

Reputation: 41

$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?

Upvotes: 2

karvin.developer
karvin.developer

Reputation: 556

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.

Upvotes: 2

Related Questions