473,396 Members | 1,992 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 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 4604
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.