SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Member
    Join Date
    Dec 2009
    Posts
    17
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Require_once() Problem - navigating file folders

    I am having troubles with require_once() in my application.

    So, I have two folders: 'lib' and 'content'. Both are at the same level in the folder structure (i.e. 'Application' > 'lib' & 'Application' > 'content').

    Under the 'lib' folder, I have a couple others:
    • lib > models
    • lib > views
    • lib > variables


    Under my 'content' folder, I have the pages that are displayed in the browser (add.php, view.php).

    On add.php, I have require_once('../lib/views/addClass.php');. When I run add.php in the browser, I get:

    Warning: require_once(./variables/variables.php) [function.require-once]: failed to open stream: No such file or directory in C:\xampp\htdocs\application\lib\models\addClass.php on line 2


    I can't figure out why it can't open the file. Any thoughts?

    I am running XAMPP on Windows 7.

  2. #2
    I solve practical problems. bronze trophy
    Michael Morris's Avatar
    Join Date
    Jan 2008
    Location
    Knoxville TN
    Posts
    2,011
    Mentioned
    57 Post(s)
    Tagged
    0 Thread(s)
    Relative paths are evil. I personally define the root path of my application as early as I can. That's probably what's going on here, check the path that PHP actually used to try to find the file and see if that's correct.

  3. #3
    SitePoint Member
    Join Date
    Dec 2009
    Posts
    17
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for the response.

    The funny thing is that I have a variables.php which I define my domain so that I can do something like require_once(DOMAIN . '/application/lib/views/add.php'); so I can avoid that but I have to pull in the variables.php file before I can use the constant.

    How can I check the path that PHP used? Are you talking about "(include_path='.;C:\xampp\php\PEAR')"?

  4. #4
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    You can carefully add the path

    C:\xampp\htdocs\application\

    ... to that ini directive, then restart the webservice (Apache?)

    However, if you do not have access to your ini file on your LIVE server then you can alter this directive using Apache instructions (in .htaccess say), and even at the page level if you have one single boot file that every public pages is handled by.

    ini_set

    Otherwise you will have to adopt another practice such as having a setting in a config file which contains the actual include path.

    There are other ways.

  5. #5
    SitePoint Addict webaddictz's Avatar
    Join Date
    Feb 2006
    Location
    Netherlands
    Posts
    295
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hello.

    Quote Originally Posted by Cups View Post
    However, if you do not have access to your ini file on your LIVE server then you can alter this directive using Apache instructions (in .htaccess say), and even at the page level if you have one single boot file that every public pages is handled by.

    ini_set
    Uhm, don't you mean set_include_path? I generally add a path like so:

    PHP Code:
    <?php
    set_include_path
    (
      
    __DIR__ PATH_SEPARATOR get_include_path( )
    );
    That works well for me.
    Yes, I blog, too.

  6. #6
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    Nope, works as I expect ... what's the benefit of set_include_path then?
    PHP Code:
    <?php
    $a 
    ini_get('include_path');
    var_dump($a);

    ini_set('include_path''esra_ym');

    echo 
    'some stuff <hr /> ';

    $b ini_get('include_path');
    var_dump($b);
    ?>
    //string '.\;\var\www\includes;\var\www\includes\PEAR;' (length=77)
    //some stuff <hr />
    //string 'esra_ym' (length=7)

  7. #7
    SitePoint Addict webaddictz's Avatar
    Join Date
    Feb 2006
    Location
    Netherlands
    Posts
    295
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Cups View Post
    Nope, works as I expect ... what's the benefit of set_include_path then?
    Hm, I guess there's more than one way to skin a cat in this instance. I didn't know your example actually would work. Another lesson learned
    Yes, I blog, too.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •