Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    Regular Coder ajhauser's Avatar
    Join Date
    Nov 2007
    Location
    Earlville. It's where Earls come from.
    Posts
    226
    Thanks
    74
    Thanked 1 Time in 1 Post

    PHP include paths

    Hello.
    If I were to use a PHP include in my site, such as:

    PHP Code:
    <!DOCTYPE ...>
    <html>
    ...
    <body>
    <?php include('/path/to/include.ext');?>
    </body>
    Can I use an absolute path instead of a relative one? On my site now I use the code:

    PHP Code:
    <?php include('includes/nav.html'); ?>
    Which will be fine until I ove a file up or down a level. I have found several topics on the web about this issue but I am new to PHP and have no friggin idea what they are talking about.

    Is an absolute path possible?
    Thanks!'-AJ

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    I think you misunderstand. The first block of code is an absolute path (starts with / for root), and the second is a relative to the current script.

    My recommendation is to always use relative where you can. But, be aware that PHP takes its cwd (current working directory) from the executing script, not the included ones.
    Code:
    /+
     + Users/
     +---+ YourSite
            +-----+ public_html
                     +---- index.php
                     + includes
                     +---- menu.php
                     +---- TemplateManager.php
    Now, if you include menu.php into index.php using include 'includes/menu.php'; and the same with templateManager, then use templateManager in menu.php like so: include 'TemplateManager.php';, this will fail. This is because the cwd of the index.php is public_html, while the menu cannot find the TemplateManager.php. Solve by creating a relative absolute path (sorry, I don't have a better name for it). This lets you attach to any file relative to the current file:
    PHP Code:
    require_once dirname(__FILE__) . '/./includes/TemplateManager.php'// Index's call
    require_once dirname(__FILE__) . '/./TemplateManager.php'// Menu's call 
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)

  • #3
    Regular Coder ajhauser's Avatar
    Join Date
    Nov 2007
    Location
    Earlville. It's where Earls come from.
    Posts
    226
    Thanks
    74
    Thanked 1 Time in 1 Post
    I should clarify again, here is an example of what I mean:

    http://www.temp1.hangnailproductions.com/index2.php

    In this example, my source code is:

    PHP Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>php</title>
    </head>

    <body>
    <?php include('http://www.temp1.hangnailproductions.com/includes/nav.html'); ?>
    </body>
    </html>
    But that include address is failing.
    Does that make sense?

    The reason I ask is because if I base every page off a template with the same include, but save the additional files in a subfolder, I don't think the include will work...

  • #4
    Regular Coder
    Join Date
    Apr 2009
    Location
    somewhere over the rainbow
    Posts
    105
    Thanks
    2
    Thanked 14 Times in 14 Posts
    i think allow_url_include is not true.


    have you read what Fou-Lu wrote???
    the "/" before files\folders will get you absolute within your web.
    include '/includes/nav.html';
    Last edited by Eran; 05-01-2009 at 11:52 AM.

  • #5
    Supreme Master coder! abduraooft's Avatar
    Join Date
    Mar 2007
    Location
    N/A
    Posts
    14,866
    Thanks
    160
    Thanked 2,224 Times in 2,211 Posts
    PHP Code:
    <?php include('http://www.temp1.hangnailproductions.com/includes/nav.html'); ?>
    Quote Originally Posted by http://php.net/function.include
    If "URL fopen wrappers" are enabled in PHP (which they are in the default configuration), you can specify the file to be included using a URL (via HTTP or other supported wrapper - see List of Supported Protocols/Wrappers for a list of protocols) instead of a local pathname. If the target server interprets the target file as PHP code, variables may be passed to the included file using a URL request string as used with HTTP GET. This is not strictly speaking the same thing as including the file and having it inherit the parent file's variable scope; the script is actually being run on the remote server and the result is then being included into the local script.
    You don't actually need to include your file via HTTP ,which is not equivalent of including the path at the server side.

    If you want to use the absolute path at the server side, use it like
    PHP Code:
    include $_SERVER['DOCUMENT_ROOT']."/includes/nav.html"
    (just echo the above serve variable to know what's your absolute file path to your roo directory at server side)

    I usually define a constant like
    PHP Code:
    define(_ROOT_$_SERVER['DOCUMENT_ROOT']); 
    at the top(or in config file, which would get included in all other files), to make it easier for typing the absolute path, like
    PHP Code:
    include _ROOT_."/includes/nav.html"
    The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)


  •  

    Posting Permissions

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