473,563 Members | 2,617 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

require_once in foreach loop

29 New Member
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. }
  10. $filename = $prefix.$inc_file;
  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. }
  20. ?>
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
  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
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
Oct 10 '10 #1
9 4614
1,044 Recognized Expert Top Contributor
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
29 New Member
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
1,044 Recognized Expert Top Contributor
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
29 New Member
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
  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. }
  11. $filename = trim($current_folder . $prefix . $inc_file);
  12. $lines = file($filename);
  13. $search = 'classes';
  14. $replace = $prefix . $search;
  16. foreach($lines as $line) {
  17.     $req_line = trim(str_replace($search, $replace, $line));
  18.     require_once $req_line;
  19. }
  21. ?>
Turns out I really recommend this approach.
Oct 12 '10 #5
8,658 Recognized Expert Moderator Expert
if you try to load PHP class definitions (seems like that), why not use autoloading?
Oct 12 '10 #6
29 New Member
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
8,658 Recognized Expert Moderator Expert
autoloading is not a 5.3 feature (that would be namespacing)
Oct 12 '10 #8
29 New Member
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
8,658 Recognized Expert Moderator Expert
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

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 findListBox.listBox2.Items) { listBox2.Items count changed, restart this foreach } Thanks for any help.
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 the loop. -- ----------------------------------- Ken Varn Senior Software Engineer Diebold Inc.
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) { Console.WriteLine(s.StudentID); }
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 foreach(Student s in course.Students) { Console.WriteLine(s.StudentID); }
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)) { some stuff....
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, because GetEnumerator always returns a new one. My proposal would allow the following scenario: You could for example the first and the last...
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 generic list of business objects. I'm able to see that the type is the correct one in the debugger. However when I try to traverse the list using the...
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 loop (the code that i have so far is below). At the instance the files are different, i want to capture one of the files and use it later on in the...
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' );
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 the iteration variable is used and you cannot modify it. For example: int pins = {9, 3, 7, 2} int newPin = 10; foreach(int pin in pins) {
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.