require_once in foreach loop

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • rythmic
    New Member
    • Feb 2010
    • 29

    require_once in foreach loop

    Why doesn't the following code work?

    Code:
    <?php
    $levels_down = 1;
    $read_length = 8192;
    $inc_file = 'classes.txt';
    $prefix = '';
    for($i = 0; $i < $levels_down;$i++) {
        $prefix .= '../';
    }
    
    $filename = $prefix.$inc_file;
    
    $lines = file($filename);
    $search = 'classes';
    $replace = $prefix . $search;
    foreach($lines as $line) {
        $req_line = str_replace($search, $replace, $line);
        require_once "$req_line";
    }
    
    ?>
    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:

    Code:
    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
    
    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
    Rythmic
  • kovik
    Recognized Expert Top Contributor
    • Jun 2007
    • 1044

    #2
    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.

    Comment

    • rythmic
      New Member
      • Feb 2010
      • 29

      #3
      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?
      Last edited by rythmic; Oct 11 '10, 09:59 PM. Reason: Incomplete

      Comment

      • kovik
        Recognized Expert Top Contributor
        • Jun 2007
        • 1044

        #4
        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. ;)

        Code:
        Failed opening required '../classes/AbstractPersistency.php '

        Comment

        • rythmic
          New Member
          • Feb 2010
          • 29

          #5
          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 :)

          Code:
          <?php
          
          $levels_down = 1;
          $read_length = 8192;
          $inc_file = 'classes.txt';
          $prefix = '';
          for($i = 0; $i < $levels_down;$i++) {
              $prefix .= '../';
          }
          
          $filename = trim($current_folder . $prefix . $inc_file);
          $lines = file($filename);
          $search = 'classes';
          $replace = $prefix . $search;
          
          foreach($lines as $line) {
              $req_line = trim(str_replace($search, $replace, $line));
              require_once $req_line;
          }
          
          ?>
          Turns out I really recommend this approach.
          Last edited by rythmic; Oct 12 '10, 07:07 AM. Reason: Adding the final resulting code

          Comment

          • Dormilich
            Recognized Expert Expert
            • Aug 2008
            • 8694

            #6
            if you try to load PHP class definitions (seems like that), why not use autoloading?

            Comment

            • rythmic
              New Member
              • Feb 2010
              • 29

              #7
              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.

              Comment

              • Dormilich
                Recognized Expert Expert
                • Aug 2008
                • 8694

                #8
                autoloading is not a 5.3 feature (that would be namespacing)

                Comment

                • rythmic
                  New Member
                  • Feb 2010
                  • 29

                  #9
                  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?

                  Comment

                  • Dormilich
                    Recognized Expert Expert
                    • Aug 2008
                    • 8694

                    #10
                    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.
                    Last edited by Dormilich; Oct 12 '10, 01:19 PM.

                    Comment

                    Working...