469,363 Members | 2,484 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,363 developers. It's quick & easy.

require_once in foreach loop

29
Why doesn't the following code work?

Expand|Select|Wrap|Line Numbers
  1. <?php
  2. $levels_down = 1;
  3. $read_length = 8192;
  4. $inc_file = 'classes.txt';
  5. $prefix = '';
  6. for($i = 0; $i < $levels_down;$i++) {
  7.     $prefix .= '../';
  8. }
  9.  
  10. $filename = $prefix.$inc_file;
  11.  
  12. $lines = file($filename);
  13. $search = 'classes';
  14. $replace = $prefix . $search;
  15. foreach($lines as $line) {
  16.     $req_line = str_replace($search, $replace, $line);
  17.     require_once "$req_line";
  18. }
  19.  
  20. ?>
  21.  
  22.  
What I'm trying to do is to use a file which lists the paths to my include files, and then I want to be able to include these files from folder levels that are different from the file where I store the include files paths.

That is if I have my list of paths in
classes.txt and I have it saved in the root folder (/)

Then I wish to access the paths listed in classes.txt from a file located in /css

To do this I must both change the paths inside the classes.txt but also reach classes.txt which is one level below the /css folder.

The code works fine until it is time to require the modified path. Although the path is correct I get this error:

Expand|Select|Wrap|Line Numbers
  1. Warning: require_once(../classes/AbstractPersistency.php ) [function.require-once]: failed to open stream: Invalid argument in D:\dev\web\project\css\inc_all_classes.php on line 17
  2.  
  3. Fatal error: require_once() [function.require]: Failed opening required '../classes/AbstractPersistency.php ' (include_path='.;C:\servers\xampp\php\PEAR') in D:\dev\web\project\css\inc_all_classes.php on line 17
  4.  
Line 17 is line 17 in the first code block.

If I replace $req_line with the path that fails to open, the require_once statement works. It just doesn't work when the path is stored in a variable.

I'm out of ideas.

I would like either a solution to this problem or an alternate path to accomplish what I'm trying to do.

Any ideas?

best regards
Rythmic
Oct 10 '10 #1

✓ answered by kovik

Did you try the realpath? You shouldn't be using URLs at all, you should be using file system paths.

By the way... I just noticed that there is a space at the end of your require. You should probably trim() that off. ;)

Expand|Select|Wrap|Line Numbers
  1. Failed opening required '../classes/AbstractPersistency.php '

9 4181
kovik
1,044 Expert 1GB
This is a long shot, but try applying realpath() to the path before attempting to require it. It will give you a better idea of what folder you are actually attempting to access, and hopefully solve your problem.
Oct 10 '10 #2
rythmic
29
I give up this approach.. It simply doesn't work. I modified the code so that it used the complete url to the file in the require statement and it still would not show. It stated it was missing a 404 redirection so I gather the file was not found, although I could paste it fine in the browser window, the very same variable.

Does anyone have a different approach?
Oct 11 '10 #3
kovik
1,044 Expert 1GB
Did you try the realpath? You shouldn't be using URLs at all, you should be using file system paths.

By the way... I just noticed that there is a space at the end of your require. You should probably trim() that off. ;)

Expand|Select|Wrap|Line Numbers
  1. Failed opening required '../classes/AbstractPersistency.php '
Oct 11 '10 #4
rythmic
29
It was the space that did it. You should not work with these things after midnight :(

Thanks for that. It will be really silly to mark that as the best answer, but hey, What can a man do :)

Expand|Select|Wrap|Line Numbers
  1. <?php
  2.  
  3. $levels_down = 1;
  4. $read_length = 8192;
  5. $inc_file = 'classes.txt';
  6. $prefix = '';
  7. for($i = 0; $i < $levels_down;$i++) {
  8.     $prefix .= '../';
  9. }
  10.  
  11. $filename = trim($current_folder . $prefix . $inc_file);
  12. $lines = file($filename);
  13. $search = 'classes';
  14. $replace = $prefix . $search;
  15.  
  16. foreach($lines as $line) {
  17.     $req_line = trim(str_replace($search, $replace, $line));
  18.     require_once $req_line;
  19. }
  20.  
  21. ?>
  22.  
Turns out I really recommend this approach.
Oct 12 '10 #5
Dormilich
8,652 Expert Mod 8TB
if you try to load PHP class definitions (seems like that), why not use autoloading?
Oct 12 '10 #6
rythmic
29
Because I have not read about that concept. and also if it is a 5.3 feature I can't use it. but I'll look into it.
Oct 12 '10 #7
Dormilich
8,652 Expert Mod 8TB
autoloading is not a 5.3 feature (that would be namespacing)
Oct 12 '10 #8
rythmic
29
ok, now I have looked into it slightly over at php.net, it seems you still need to define the require_once statement? And that would still lead to path problems?
Oct 12 '10 #9
Dormilich
8,652 Expert Mod 8TB
autoloading simply means that if you call a class, PHP will try all specified loading functions (right, you can specify more than one function to load a file). should neither function work, you’ll get a fatal error ("could not load class"), if one function fails loading, nothing happens (there are more functions to try, so throwing an include failed error makes no sense). nevertheless, the exact implementation is left to you.
Oct 12 '10 #10

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

18 posts views Thread by Ken Varn | last post: by
3 posts views Thread by rhaazy | last post: by
10 posts views Thread by fig000 | last post: by
1 post views Thread by Perl Beginner | last post: by
reply views Thread by zhoujie | last post: by
1 post views Thread by Marylou17 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.