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

Asked

Viewed 800 times

4

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

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

<?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);

?>

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'

  • 1

    Thanks for your help.

  • If the answer helped you, don’t forget to validate it =)

  • @Caiofelipepereira failed to explain the function of RecursiveDirectoryIterator.

  • 1

    @gmsantos http://php.net/manual/en/class.recursivedirectoryiterator.php

  • @gmsantos guess that wasn’t quite the scope of the question, why I just inked the documentation

  • @caio suggested this to make the answer richer :) besides solving the doubt I like to explain the pq I used method x.

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.