Imaulle Posted January 11, 2011 Share Posted January 11, 2011 Hello, I'm trying to make a little piece of code that will search for all files (including subdirectories) ending with .txt and set them to 777 foreach (glob("*.txt") as $file) { system("chmod 777 '$file'"); } Am I on the right path? thanks Link to comment https://forums.phpfreaks.com/topic/224042-using-glob-to-look-for-all-txt-in-directory/ Share on other sites More sharing options...
nderevj Posted January 11, 2011 Share Posted January 11, 2011 Close, however that will only get you the files in the current directory (not in any sub-directories). Try this: <?php $directory = 'sample'; // Replace with your starting directory $it = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($directory)); while($it->valid()) { if(strtolower(substr($it->getFilename(), -4)) == '.txt') { var_dump($it->key()); // Not really needed, just used for debugging chmod($it->key(), 0777); } $it->next(); } Link to comment https://forums.phpfreaks.com/topic/224042-using-glob-to-look-for-all-txt-in-directory/#findComment-1157766 Share on other sites More sharing options...
Imaulle Posted January 11, 2011 Author Share Posted January 11, 2011 that worked perfectly! thank you! Link to comment https://forums.phpfreaks.com/topic/224042-using-glob-to-look-for-all-txt-in-directory/#findComment-1157773 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.