PHP – CHMOD a directory recursively


The script below loops over the specified directory and chmods its files, directories and subdirectories recursively.

<?php
  function chmodDirectory( $path = ‘.’, $level = 0 ){  
    $ignore = array( ‘cgi-bin’, ‘.’, ‘..’ ); 

    $dh = @opendir( $path ); 

    while( false !== ( $file = readdir( $dh ) ) ){ // Loop through the directory 

      if( !in_array( $file, $ignore ) ){
        if( is_dir( “$path/$file” ) ){
          chmod(”$path/$file”,0777);
          chmodDirectory( “$path/$file”, ($level+1));
        } else {
          chmod(”$path/$file”,0777); // desired permission settings
        }//elseif 

      }//if in array 

    }//while 

    closedir( $dh ); 

  }//function

chmodDirectory(”the_directory/”,0);
?>

source:

http://bjw.co.nz/developer/php/61-php-chmod-a-directory-recursively

One thought on “PHP – CHMOD a directory recursively

  1. Hi there,

    Have tried your script, my server returns,

    Parse error: parse error, unexpected ‘.’, expecting ‘)’ in /home/b/a/bastim/public_html/yeah/stuff/chmod.php on line 36

    am I supposed to give this script the location of the folder,
    I guessed it returns the path of the folder it is run from.

    Have not found a script that works, maybe my server package does not allow this ?

    Is there another way of doing this, can you ftp with php and change the permissions with in that protocol ?

    Thanks for your help.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s