php include files from directories other than root directory

10,410

Solution 1

You can see that in all PHP frameworks (at least professionals) a variable/constant as APPPATH, BASEPATH etc. So you need to define your similar variable/constant in your project and use it as needed.

<?php
// a.php: assuming this included everywhere at very first line 
// and located in root directory
// preferable, define a constant instead of variable, cos it 
// may used in functions directly without "global $ROOT";

// for "include"
define('ROOT', __DIR__); // for PHP >= 5.3
define('ROOT', realpath(dirname(__FILE__)));  // for PHP < 5.3
// for "src, href" etc
define('HTTP', $_SERVER['SERVER_NAME']); // for local development support i.e foo.com.local

// includes
include ROOT .'/layout_header.php';
include ROOT .'/classes/mailer.php';
include ROOT .'/foo/bar/baz.php';
...

// src,href
printf('<script src="%s/js/move.js" />', HTTP);
printf('<img src="%s/img/lollipop.png" />', HTTP);

print '<a href="'. HTTP .'/aFolder/movement.php">Click here!</a>';
...
?>

Solution 2

Always use $_SERVER['DOCUMENT_ROOT'].'path/to/file' for including php files when it's relative.

And for images and script files and css files use yourdomain.com/path/to/file.extension

Solution 3

Have you tried using complete root instead of relative.

define('WEB_ROOT', 'http://yourdomain.com/');  // localhost path etc


echo "<img src=".WEB_ROOT."img/lollipop.png'/>";
echo "<script src=".WEB_ROOT."'js/move.js'/>";
echo "<a href='".WEB_ROOT."aFolder/movement.php'>Click here!</a>";
Share:
10,410
JTJM
Author by

JTJM

Updated on June 05, 2022

Comments

  • JTJM
    JTJM almost 2 years

    I've got a global template page, /layout_header.php as well as some other file stored in /aFolder/index.php.

    In /layout_header.php, I have:

    <?php
        // Reference to scripts and other files
        echo "<img src='img/lollipop.png'/>";
        echo "<script src='js/move.js'/>";
        echo "<a href='aFolder/movement.php'>Click here!</a>";
    ?>
    

    In /aFolder/index.php, I have:

    <?php
        include "../layout_header.php";
    ?>
    

    Now, /aFolder/index.php has no problems calling the /layout_header.php file. Unfortunately, because the directory is now /aFolder, I face the following problems:

    • Unable to view the image, since image is now linked as /aFolder/img/lollipop.png (which does not exist)
    • Unable to execute script because of reason listed above.
    • Invalid URL because of reason listed above.

    Many of my scripts and css files are using relative path. In other words, if index.php was placed in the root folder, everything works fine.

    How should I go about solving the problem of files in subdirectories?

  • JTJM
    JTJM about 11 years
    $_SERVER['DOCUMENT_ROOT'] prints /home/<user>/public_html/ - is that supposed to be correct?
  • Ankur Sinha
    Ankur Sinha about 11 years
    Yes, that is correct :) So from public_html/yoursite/whateverdirectory/whateverfile.ext
  • Ankur Sinha
    Ankur Sinha about 11 years
    So if you have a file to be included by traversing back a directory or two, you can use $_SERVER thing and then include. You will have no issues at all :)
  • JTJM
    JTJM about 11 years
    Thank you! Is there any reason why I should use ROOT instead of HTTP for including php files?
  • K-Gun
    K-Gun about 11 years
    It's just an alias, you can change it as you like. I used it cos more meaning (root: directory root, http: source address). Also, you can't include files from http.