PHP loop with require

scoutch29

Junior Member
Aug 8, 2010
17
0
0
Hi,

I'm encountering problems with PHP v5.3.2. Does anyone encounters any problems when requiring / including a PHP file while listing files from a loop ? Right now, I can only output one file below the require / include line but on the above, everything outputs as expected.

If anyone could shed me some light on this would be greatly appreciated.
 
Last edited:

Cogman

Lifer
Sep 19, 2000
10,283
134
106
Could you post a piece of sample code for what you are trying to do?

If you are trying something like

while (someCondition)
include file[index];

That won't work. include literally copy and pastes the file in at the location of the include. In other words, you can't include multiple files by putting an include in a loop.
 
Last edited:

scoutch29

Junior Member
Aug 8, 2010
17
0
0
Like this:

PHP:
<?php

$folder = @opendir('path'); // 'path' for example.
while (false !== ($file = @readdir($folder))) {
   if ($file != '..' && $file != '.') {
      require('path/' . $file);
   }
}
When doing this, supposing I have 3 results, only the 2 firsts will appear. Even by using foreach(glob(, I get the same results.

Any way I could resolve this ?
 

Cogman

Lifer
Sep 19, 2000
10,283
134
106
To make sure I understand you correctly, you want to list every file that is in a folder and its sub folders, correct? In other words, you want to access

/foo
/moo
/coo
/stu
and
/bar/foo
/bar/boo
/bar/doo

but you are only seeing
/foo
/moo
/coo
/stu

If that is the case, then http://www.hawkee.com/snippet/1281/ might be what you are looking for.
 

scoutch29

Junior Member
Aug 8, 2010
17
0
0
To make sure I understand you correctly, you want to list every file that is in a folder and its sub folders, correct? In other words, you want to access

/foo
/moo
/coo
/stu
and
/bar/foo
/bar/boo
/bar/doo

but you are only seeing
/foo
/moo
/coo
/stu

Even worst. I only see /foo /moo but if I, either, remove the require line and echo the results, I can see all the results also when I leave the echo above the require line in the loop. The problem is with the require line and I output the results of $file below. I only see the two first results for an unknown reason.
 

Cogman

Lifer
Sep 19, 2000
10,283
134
106
Even worst. I only see /foo /moo but if I, either, remove the require line and echo the results, I can see all the results also when I leave the echo above the require line in the loop. The problem is with the require line and I output the results of $file below. I only see the two first results for an unknown reason.

Give this a try, replace your require line with this

$fileStr = 'path/' . $file;
eval('require $fileStr;');

(is that how you do string concatenation with php? It's been so long.)
 

Cogman

Lifer
Sep 19, 2000
10,283
134
106
ok, how about this.

before the while loop have this line
PHP:
$fileStr = '';

in the while loop have this line
PHP:
$fileStr .= 'require \'path/' . $file . '\';\n';

and after the while loop have this line
PHP:
eval('$fileStr');

If this doesn't work, post what an echo of $fileStr after the loop.
 
Last edited:

Cogman

Lifer
Sep 19, 2000
10,283
134
106
BTW, I just edited the code to put a newline after the ;... So if you saw it before the edit, it will probably fail.
 

scoutch29

Junior Member
Aug 8, 2010
17
0
0
Well, what do you know, it worked for all the results now. Although, when I add the \n at the end of the line in the loop, it produces a T_CONSTANT_ENCAPSED_STRING error also stating that the target class name cannot be found.

If I do remove the \n, no errors will show up, all the results will show from the output but I cannot output anything from the classes . . . (yes, I'm trying to import classes).

Any more ideas ?
Thanks for all your help so far, btw.
 

Cogman

Lifer
Sep 19, 2000
10,283
134
106
Well, I can't comment on the classes or their functionality. (as for the newline thing... yea, don't know what to tell yeah, it should work if it looks like my code above. If not. Whatever, not THAT big of an issue).

You might run into issues because you treating php and the required statement in a way that it was never meant to be used. php might not be smart enough to realize you have new classes even after executing the statement.

Generally, you should avoid code like this as it can be riddled with such issues. It is usually better to just include the classes you need explicitly and have them include the classes they need.
 

scoutch29

Junior Member
Aug 8, 2010
17
0
0
I do have an unexpected $end error message when using the quotes amongst the eval function. It also states that eval'(d) code on line 1 ...

When I remove the quotes, its blank but so blank that I can't output the results from the class either ...

Any more ideas ?
 

Cogman

Lifer
Sep 19, 2000
10,283
134
106
I do have an unexpected $end error message when using the quotes amongst the eval function. It also states that eval'(d) code on line 1 ...

When I remove the quotes, its blank but so blank that I can't output the results from the class either ...

Any more ideas ?

opps, those quotes shouldn't have been there Like I said, its been a while since I've used php (yeah.. that will work for an excuse...)

If you just regularly include/require the files, can you output the results?
 

scoutch29

Junior Member
Aug 8, 2010
17
0
0
opps, those quotes shouldn't have been there Like I said, its been a while since I've used php (yeah.. that will work for an excuse...)

If you just regularly include/require the files, can you output the results?

No problem for the quotes. I have just removed them. As for the regular require and include, yes - of course, it works normally. It's just when using it inside a loop and even putting the values in an array, the results won't output as expected as a strange case scenario lol !
 

Cogman

Lifer
Sep 19, 2000
10,283
134
106
No problem for the quotes. I have just removed them. As for the regular require and include, yes - of course, it works normally. It's just when using it inside a loop and even putting the values in an array, the results won't output as expected as a strange case scenario lol !

well, it really isn't that strange. The require/include statement are meant to be used as preprocessor statements (IE used before any other code is executed), not as general functions. Their exact meaning is "Take code from this file and put it here". Php may be having problem with the include because it is expecting to setup all the class stuff before actually executing code. In this case, it is executing code and is supposed to setup the class. But, apparently, we've stumbled on a nice php bug.
 

Cogman

Lifer
Sep 19, 2000
10,283
134
106
No problem, thats what this forum is here for . Someone else will probably chime in if there is a better solution. I myself am not a php expert.
 
sale-70-410-exam    | Exam-200-125-pdf    | we-sale-70-410-exam    | hot-sale-70-410-exam    | Latest-exam-700-603-Dumps    | Dumps-98-363-exams-date    | Certs-200-125-date    | Dumps-300-075-exams-date    | hot-sale-book-C8010-726-book    | Hot-Sale-200-310-Exam    | Exam-Description-200-310-dumps?    | hot-sale-book-200-125-book    | Latest-Updated-300-209-Exam    | Dumps-210-260-exams-date    | Download-200-125-Exam-PDF    | Exam-Description-300-101-dumps    | Certs-300-101-date    | Hot-Sale-300-075-Exam    | Latest-exam-200-125-Dumps    | Exam-Description-200-125-dumps    | Latest-Updated-300-075-Exam    | hot-sale-book-210-260-book    | Dumps-200-901-exams-date    | Certs-200-901-date    | Latest-exam-1Z0-062-Dumps    | Hot-Sale-1Z0-062-Exam    | Certs-CSSLP-date    | 100%-Pass-70-383-Exams    | Latest-JN0-360-real-exam-questions    | 100%-Pass-4A0-100-Real-Exam-Questions    | Dumps-300-135-exams-date    | Passed-200-105-Tech-Exams    | Latest-Updated-200-310-Exam    | Download-300-070-Exam-PDF    | Hot-Sale-JN0-360-Exam    | 100%-Pass-JN0-360-Exams    | 100%-Pass-JN0-360-Real-Exam-Questions    | Dumps-JN0-360-exams-date    | Exam-Description-1Z0-876-dumps    | Latest-exam-1Z0-876-Dumps    | Dumps-HPE0-Y53-exams-date    | 2017-Latest-HPE0-Y53-Exam    | 100%-Pass-HPE0-Y53-Real-Exam-Questions    | Pass-4A0-100-Exam    | Latest-4A0-100-Questions    | Dumps-98-365-exams-date    | 2017-Latest-98-365-Exam    | 100%-Pass-VCS-254-Exams    | 2017-Latest-VCS-273-Exam    | Dumps-200-355-exams-date    | 2017-Latest-300-320-Exam    | Pass-300-101-Exam    | 100%-Pass-300-115-Exams    |
http://www.portvapes.co.uk/    | http://www.portvapes.co.uk/    |