danielneil Posted November 11, 2011 Share Posted November 11, 2011 Hi Everyone, I'm trying to use a while loop to dynamically include() .php files if a preg_match condition concerning the file name returns true, e.g. if ( $hDirectory = opendir($_SERVER["DOCUMENT_ROOT"])) { while (false !== ($strFileName = readdir($hDirectory))) { if ( pathinfo($strFileName, PATHINFO_EXTENSION ) == "php" ) { if ( preg_match("/frm_/", $strFileName) ) { include_once $strFileName; } } echo $strFileName; } closedir($hDirectory); } But, it doesn't seem to work as I thunked it would - the loop iterates several times but then unexpectedly exits, which is wrong because there are hundreds of .php files with 'frm_' in the filename!. Thoughly, as I did expect, should I comment out the include() statement all files names get echoed! and things are happy!!!! Analogiciously, I'm doing this because I'm lazy and want to be able to 'drop' .php files into the same directory and have my script automagically include them in my project save me from writing another freaking include statement! Am going about this the wrong way? If so, how did y'all get around the tedious task of having to write hundreds of bloody include() statements in your projects? I've seen this question asked a few times in Google-land, but not answered, which leds me to believe I'm doing something deliciously-evil. Cheers, Daniel Quote Link to comment https://forums.phpfreaks.com/topic/250950-include-nested-within-a-loop/ Share on other sites More sharing options...
PFMaBiSmAd Posted November 11, 2011 Share Posted November 11, 2011 A) You can use glob to get just a list of files you want glob("frm_*.php");. You would then simply iterate over the array that glob returns and include the files. B) You likely have a fatal parse or runtime error in the code being included. Do you have error_reporting set to E_ALL and display_errors set to on so that php will help you by reporting and displaying all the errors it detects in your code? Quote Link to comment https://forums.phpfreaks.com/topic/250950-include-nested-within-a-loop/#findComment-1287433 Share on other sites More sharing options...
danielneil Posted November 11, 2011 Author Share Posted November 11, 2011 A) Awesome! That's worked. B) Nope, E_ALL wasn't giving me anything Anyway, if anyone else is interested in this, my implementation went something like this: foreach (glob("*.php") as $fileName) { if (( preg_match("/class_/", $fileName) ) || ( preg_match("/frm_/", $fileName)) ) { if ( preg_match("/frm_admin/", $fileName)) { if (!$user->isAdmin()) { continue } } include($fileName); } } This is interesting because using this I can also limit which .php files are included in my script based on who the user is! E.g. if (!$user->isAdmin()) { don't include the frm_admin files!; } Many Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/250950-include-nested-within-a-loop/#findComment-1287438 Share on other sites More sharing options...
trq Posted November 11, 2011 Share Posted November 11, 2011 Am going about this the wrong way? There are better way's of doing this if you are using an object oriented approach. See spl_autoload Quote Link to comment https://forums.phpfreaks.com/topic/250950-include-nested-within-a-loop/#findComment-1287439 Share on other sites More sharing options...
danielneil Posted November 11, 2011 Author Share Posted November 11, 2011 I did come across that, but it wasn't useful in this case as not all the files represent classes. Quote Link to comment https://forums.phpfreaks.com/topic/250950-include-nested-within-a-loop/#findComment-1287441 Share on other sites More sharing options...
kicken Posted November 12, 2011 Share Posted November 12, 2011 Aside from an error which should be shown provided your error_reporting and display_errors settings are correct, the only other real explanations would be - One of the files is calling exit; or die; and killing the process - One of the files is throwing an exception which is being caught somewhere but not reported. Quote Link to comment https://forums.phpfreaks.com/topic/250950-include-nested-within-a-loop/#findComment-1287504 Share on other sites More sharing options...
MasterACE14 Posted November 12, 2011 Share Posted November 12, 2011 I did come across that, but it wasn't useful in this case as not all the files represent classes. You can separate the files, use glob() for the files that aren't classes and autoload for the files that have classes. That way you're only including what you need, rather than everything. Quote Link to comment https://forums.phpfreaks.com/topic/250950-include-nested-within-a-loop/#findComment-1287506 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.