Jump to content

include() nested within a loop


danielneil

Recommended Posts

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

 

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

 

 

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.