SitePoint Sponsor

User Tag List

Results 1 to 9 of 9
  1. #1
    SitePoint Enthusiast
    Join Date
    Feb 2009
    Posts
    27
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    require_once not finding file

    I've structured my sites by defining functions that build certain elements within the site. I then simply call the relevant functions depending on the page. To make things simpler I have a page called holding_fns.php which simply uses the require_once function to include different subsets of functions ie. user authentication functions, display functions etc. That way each page only needs to include this one script in order to include all the functions it might need. HOWEVER...
    I have a set of functions contained within a file called db_fns.php. This (obviously!) contains functions that relate to the database - specifically the function that opens a connection with the database. As such it has a username and password combination to allow this.
    I would like, for obvious reasons, to move this file out of the public_html tree on my hosted server. I thought I could do this by using the following code:

    require_once('../db_fns.php');

    but for some reason this breaks the holding_fns.php script.
    I thought this was straight forward. Any ideas why it's not working?!?!

  2. #2
    Programming Team silver trophybronze trophy
    Mittineague's Avatar
    Join Date
    Jul 2005
    Location
    West Springfield, Massachusetts
    Posts
    17,169
    Mentioned
    190 Post(s)
    Tagged
    2 Thread(s)
    If the file can't access the database from outside the root, it should be able to pull the data from outside the root. That is, db_fns.php could require_once "../db_data.php"

  3. #3
    SitePoint Enthusiast
    Join Date
    Feb 2009
    Posts
    27
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks Mittineague. I think I understand what you're getting at. However, I'm not sure that's the problem.
    The file db_fns file only has one function in it - the function that is called when a database connection is required. Surely this means that even if, as you say, the database can't be accessed from outside the root it doens't matter cos all I'm by the time the function is called it will have been required into another file. Does that make sense?
    Are you suggesting I place the information needed to connect as variables in a file db_data.php, place that outside the root, then request that file into db_fns and use the variables within the function?

  4. #4
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,500
    Mentioned
    163 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by tbox View Post
    However, I'm not sure that's the problem.
    Try it

    Btw, right now, are you getting any error?

  5. #5
    SitePoint Enthusiast
    Join Date
    Feb 2009
    Posts
    27
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Just tried it and it's still not happy. Because the function in db_fns now includes the require_once for db_data the site only breaks once the function is called. Still basically the same problem though. Getting a bit frustrating... Isn't this that any site with a database has to do?!?!?!

  6. #6
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,500
    Mentioned
    163 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by tbox View Post
    the site only breaks once the function is called
    What error do you get?

  7. #7
    SitePoint Enthusiast
    Join Date
    Feb 2009
    Posts
    27
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Warning: require_once(../../holding_db_data.php) [function.require-once]: failed to open stream: No such file or directory in /home/tourbox1/public_html/includes/holding_db_fns.php on line 4

    Fatal error: require_once() [function.require]: Failed opening required '../../holding_db_data.php' (include_path=''.:/usr/lib/php:/usr/local/lib/php:/home/tourbox1/'') in /home/tourbox1/public_html/includes/holding_db_fns.php on line 4

  8. #8
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2008
    Posts
    5,757
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    To help you understand relative filepaths and how php tries to resolve them, make use of getcwd(). The value returned by getcwd() is the directory which php will try to resolve a relative file path from(well, it may also try to use the include path too, but lets ignore that for now).

    For example,
    PHP Code:
    echo getcwd() . '/../../holding_db_data.php';
    require_once (
    '../../holding_db_data.php'); 
    So if the value of getcwd() was
    /home/foo/bar/baz

    Then ../../holding_db_data.php
    would resolve to /home/foo/holding_db_data.php


    Keep in mind, when you include a file, it does not change php's current working dir. The current working directory is generally the directory of the first file executed.

  9. #9
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2006
    Location
    Augusta, Georgia, United States
    Posts
    4,151
    Mentioned
    16 Post(s)
    Tagged
    3 Thread(s)
    If your host support $_SERVER['DOCUMENT_ROOT'] use it then remove the last directly and use it as your private root. Then anything that is above the site root simple place them together and you have a nice absolute path. Simple example:

    PHP Code:
    define('PUBLIC_ROOT',$_SERVER['DOCUMENT_ROOT']);
    define('PRIVATE_ROOT',str_replace('/httpdocs','',PUBLIC_ROOT));

    require_once(
    PRIVATE_ROOT.'/db_fns.php'); 
    where /httpdocs is the site root directory.


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
  •