1

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.

Ryan Vincent
  • 4,483
  • 7
  • 22
  • 31
  • Remember that you need write permissions on this files and directories, else you cannot modify it. If you execute it with apache user then the apache user need this permissions – Sal00m Apr 29 '15 at 13:03

2 Answers2

2

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.

Stephan Weinhold
  • 1,623
  • 1
  • 28
  • 37
2
$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?

Stephan Weinhold
  • 1,623
  • 1
  • 28
  • 37