472,371 Members | 1,552 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,371 software developers and data experts.

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 4480
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,658 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,658 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,658 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

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

Similar topics

13
by: TrintCSD | last post by:
How can I reset the collections within a foreach to be read as a change from within the foreach loop then restart the foreach after collections has been changed? foreach(string invoice in...
18
by: Ken Varn | last post by:
Is there any way to reset a foreach loop to re-iterate through the collection as if it were starting from the beginning? Namely, if I delete an item out of a collection, I want to be able to reset...
5
by: David C | last post by:
This is very strange. Say I have code like this. I am simply looping through a collection object in a foreach loop. Course course = new Course(); foreach(Student s in course.Students) {...
25
by: David C | last post by:
I posted this question, and from the replies, I get the impression that I worded my posting very poorly, so let me try this again. While debugging and stepping through this foreach loop ...
3
by: rhaazy | last post by:
If I want to use a FOREACH loop on an array of strings, but didn't want to include the first member,how would I do this??? Psuedo Code::: foreach(string a in myStringArray (where a.index>0))...
3
by: cody | last post by:
Currently it is only legal to use types which has a method named GetEnumerator to be used in a foreach loop. This makes it impossible to use the same Enumerator after and before a foreach loop,...
10
by: fig000 | last post by:
HI, I'm new to generics. I've written a simple class to which I'm passing a generic list. I'm able to pass the list and even pass the type of the list so I can use it to traverse it. It's a...
1
by: Perl Beginner | last post by:
I hope i can articulate this question properly. i have been trying to figure this out for over a week. I am comparing the contents of two files, but the comparison is done inside of a foreach...
3
by: SM | last post by:
Hello, I have an array that holds images path of cd covers. The array looks like this: $cd = array( 589=>'sylver.jpg', 782=>'bigone.jpg', 158=>'dime.jpg' );
1
by: greyseal96 | last post by:
Hi, I am a pretty new programmer, so I apologize in andvance if this is a dumb question... In a book that I'm reading to learn C#, it says that when using a foreach() loop, a read-only copy of...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge required to effectively administer and manage Oracle...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was proposed, which integrated multiple engines and...
1
by: Matthew3360 | last post by:
Hi, I have been trying to connect to a local host using php curl. But I am finding it hard to do this. I am doing the curl get request from my web server and have made sure to enable curl. I get a...
0
Oralloy
by: Oralloy | last post by:
Hello Folks, I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA. My problem (spelled failure) is with the synthesis of my design into a bitstream, not the C++...
0
by: Carina712 | last post by:
Setting background colors for Excel documents can help to improve the visual appeal of the document and make it easier to read and understand. Background colors can be used to highlight important...
2
by: Ricardo de Mila | last post by:
Dear people, good afternoon... I have a form in msAccess with lots of controls and a specific routine must be triggered if the mouse_down event happens in any control. Than I need to discover what...
1
by: Johno34 | last post by:
I have this click event on my form. It speaks to a Datasheet Subform Private Sub Command260_Click() Dim r As DAO.Recordset Set r = Form_frmABCD.Form.RecordsetClone r.MoveFirst Do If...
1
by: ezappsrUS | last post by:
Hi, I wonder if someone knows where I am going wrong below. I have a continuous form and two labels where only one would be visible depending on the checkbox being checked or not. Below is the...
0
by: jack2019x | last post by:
hello, Is there code or static lib for hook swapchain present? I wanna hook dxgi swapchain present for dx11 and dx9.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.