Forum Moderators: coopster

Message Too Old, No Replies

iterate through directory

         

scorpion

10:50 pm on Apr 26, 2003 (gmt 0)

10+ Year Member



Does anybody know how to iterate through each file in a directory and changes the file permissions of each file in php? Also you only want to do this for files with a certain extension. (i.e. not your .php script of course)

andreasfriedrich

11:32 pm on Apr 26, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You will need to obtain a directory handle using opendir [php.net]. Then calling readdir [php.net] will return all filenames in that directory. If you are done closedir [php.net] will close the directory handle.


$dir = '/path/to/files';
if ($handle = opendir [php.net]($dir)) {
while(false!== ($file = readdir [php.net]($handle))) {
if ($file == '.' && $file == '..') continue;
if(!is_file [php.net]($dir.$file) && preg_match [php.net]("'\.(html¦jpe?g¦gif)$'", $file)) continue;
chmod [php.net]($dir.$file, 0777);
}
closedir [php.net]($handle);
}

Andreas

scorpion

12:40 am on Apr 27, 2003 (gmt 0)

10+ Year Member



cool, thanks alot!