www.webdeveloper.com
Results 1 to 8 of 8

Thread: using <?php include.....?>

  1. #1
    Join Date
    Mar 2013
    Posts
    5

    using <?php include.....?>

    I am trying to set up a web site in PHP (which is somewhat new to me). On my local machine, it all works just fine. But on the server things don't work OK and I'm not sure why.

    My document root is public_html
    off of that, I have includes, jpgs and a few other folders. SO, it's
    public_html
    includes
    jpgs

    I would like to be able to reference files in the includes folder without a direct path from where it is being referenced.

    SO, something like <?php include('includes/xxx.php'); ?>

    current I include the absolute path from the document root and that seems to work but it feels clunky. HOWEVER, on the server, this also does not work. On the server, where the documen root is public_html, I have to go ../../incudes/xxx.php, but if the file containing the reference is nested further I would have to have ../../../ etc.

    I've tried:

    Code:
    <?php
    
    $paths[] = '.';
    $paths[] = 'public_html/includes/';
    $paths[] = '/jpgs';
    $paths[] = get_include_path();
    
    set_include_path(join(PATH_SEPARATOR, $paths));
    ?>
    BUT I still cannot access files in the include folder from any place other than public_html folder.

    RON

  2. #2
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,251
    You can use $_SERVER['DOCUMENT_ROOT'] to create a consistent starting point for any file path, e.g.:
    PHP Code:
    include $_SERVER['DOCUMENT_ROOT'].'/path/to/file.php'
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  3. #3
    Join Date
    Mar 2013
    Posts
    5
    I get a permission denied when I do that.

  4. #4
    Join Date
    Mar 2013
    Posts
    5
    Here's an example of the problem: I am using a header.php file. In that file I have a reference to an image, the jpgs folder (public_html/jpgs). If I reference that from a top level file, it's OK. BUT if the file is in another nested folder, like, say, in public_html/gallery/newyork then the 'src=jpgs/image.jpg' won't work (of course). I would like to have an absolute path from public_html so I don't have to worry about where these files are being accessed.

  5. #5
    Join Date
    Mar 2013
    Posts
    5
    Now my top level path is:
    /home/xerxes11/public_html/

    and from the top level (i.e., public_html) I can access the jpg folder which is public_html/jpgs BUT if I'm in a lower folder like:

    public_html/gallery/newyork the the path /home/xerxes11/public_html/jpgs can't be found. This is what I find confusing.

  6. #6
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,251
    Quote Originally Posted by RonNYC View Post
    I get a permission denied when I do that.
    It would help to see the exact code that causes this error along with a copy of the complete error message.

    As far as replies #4 and #5 go, HTML SRC attributes are different than PHP file system paths (such as include() would use). For SRC (or HREF) attributes, you can simply indicate it's relative to the document root with a leading slash:
    PHP Code:
    echo "<img src='/jpgs/image.jpg' alt='' />"
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  7. #7
    Join Date
    Mar 2013
    Posts
    5

    I fixed some things

    But it seems cludgey to me.

    The paths (hrefs and src) to the various files are now //myserver.com/~xerxes11/jpgs/

    and that works.

    And for the includes:

    include('/home/xerxes11/public_html/includes/header.php'); ?>

    but the /home/xerxes11 part bothers me. I just feel it should be either /includes if public_html is the document root or public_html/includes/ OH WELL, it does work. SORRY TO BE A BOTHER!

    RON

  8. #8
    Join Date
    May 2011
    Posts
    101
    Quote Originally Posted by RonNYC View Post
    I get a permission denied when I do that.
    NogDog is absolutely right.

    include $_SERVER['DOCUMENT_ROOT'].'/path/to/file.php';

    is the consistent way to ensure correct paths.

    If you have permissions problems, you need to first sort out the chmod on those
    directories to which you are being denied access.

    C
    Sometimes, when you re-invent the wheel, you end up with a better wheel.
    ---------------------------------------------
    One-on-One Scripting Tuition/Mentoring

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

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



Recent Articles