Imad Posted July 19, 2008 Share Posted July 19, 2008 Hi Guys, I'm trying to achieve a little script to scan a directory and bring back php files only, here's what I have so far: if ($handle = opendir('versions')) { echo "Files:\n"; while (false !== (preg_match("/\.php([^\.php]+)$/", $file) && $file = readdir($handle))) { echo "$file\n"; } return $file; } It doesn't seem to return any php files in the directory. Any ideas why? Best Regards. Quote Link to comment Share on other sites More sharing options...
unkwntech Posted July 19, 2008 Share Posted July 19, 2008 # \.php([^\.php]+)$ # # Match the character “.” literally «\.» # Match the characters “php” literally «php» # Match the regular expression below and capture its match into backreference number 1 «([^\.php]+)» //what is the purpose for this # Match a single character NOT present in the list below «[^\.php]+» # Between one and unlimited times, as many times as possible, giving back as needed (greedy) «+» # A . character «\.» # One of the characters “ph” «php» # Assert position at the end of the string (or before the line break at the end of the string, if any) «$» If you just trying to check if the file has a .php extension then use this: '/\.php./' Quote Link to comment Share on other sites More sharing options...
DarkWater Posted July 19, 2008 Share Posted July 19, 2008 How about you use: foreach (glob('somedir/*.php') as $filename) { echo filesize($filename) . "<br />"; } Quote Link to comment Share on other sites More sharing options...
Imad Posted July 19, 2008 Author Share Posted July 19, 2008 Thanks for your help guys, I figured I'd put the substr to use and created this: $directory = @opendir("files"); if($directory) { while($modules = @readdir($directory)) { //file extension || substr: part of file || strrchr: end of char $extention = substr(strrchr($files, "."), 1); //Check to see if php is after the period if($extention == "php") { $files_list[] = $files; } } sort($files_list); } @closedir($directory); return $files_list; } Quote Link to comment Share on other sites More sharing options...
DarkWater Posted July 19, 2008 Share Posted July 19, 2008 Just use glob().... foreach (glob('versions/*.php') as $filename) { $module_list[] = $filename; include("versions/$filename"); } Quote Link to comment Share on other sites More sharing options...
Imad Posted July 19, 2008 Author Share Posted July 19, 2008 Just use glob().... foreach (glob('versions/*.php') as $filename) { $module_list[] = $filename; include("versions/$filename"); } Will do then. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.