How to apply chmod 777 with php in main folder and several subfolders at the same time

4

I would like to know how I can apply with a single code in php chmod 777 in a folder and subfolders at one time in case I have a folder called images and inside it has 4 subfolders I need to perform chmod 777 on them with a single code instead of using the code below.

And is there any way you could apply chmod 777 in the images folder and all its subfolders with a single code in the case?

<?php
$chmod = chmod("imagens", 0777);
$chmod = chmod("imagens/medias", 0777);
$chmod = chmod("imagens/capas", 0777);
$chmod = chmod("imagens/icones", 0777);
$chmod = chmod("imagens/screen_shots", 0777);

?>
    
asked by anonymous 22.05.2015 / 16:50

1 answer

5

Using recursion. There is a class called RecursiveIteratorIterator that solves your problem. See the example taken from php.net

<?php

    $iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($pathname));

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

?>

In your case, $filemode = 0777 and $pathname = 'imagens'

    
22.05.2015 / 16:56