• PHP – absolute file paths in HTML

    WinMan Member

    Hi, I know how to create absolute filepaths in PHP, but do I need to do the same thing when linking to files in HTML?


    require_once dirname(__FILE__).'/includes/config.inc.php';



    When I mess around with the HTML above e.g. by duplicating the dirnames e.g. dirname(dirname(__FILE__))?>"/includes/config.inc.php>" I still get to config.inc.php.

    Also, are the backslashes for a new folder performed in the PHP or the HTML?

  • Abhey Member

    For starters, files get loaded differently in PHP (From the webserver) and HTML (From the clients computer). When you try to include a file through PHP, you will be talking to your webserver locally. This means that the entire disk is accessable (or so to say). When you’re trying to use dirname(__FILE__) (__DIR__ is available since PHP 5.3) the function will return the path of the file from the disk. It would look something like this on linux ‘ /home/user/public_html/ ‘. When an user visits your website in a webbrowser, all the files get loaded relatively from the URL.

    So let’s take your link and see what happens:
    The link would point to something like: ‘/home/user/public_html/includes/config.php’. (Since that’s what the function dirname returned.
    This will make the web browser believe that it needs to go to


    Which, of course, cannot be found.

    Now that you know this, it is easy to conclude what to point the link to. It would be /includes/config.php. As that is what the location of the file is relatively to the URL.

  • Adelaid Member

    So all file locations which require server-side manipulation can be written in the script using functions like dirname(__FILE__)./includes/config.php but any links in the html should avoid these kind of absolute paths embedded into them via PHP tags and should just refer to the file link relatively e.g. /includes/config.php.

  • SapnaVishwas Member

    You could refer to the base url of your website instead of an absolute directory

Viewing 3 reply threads
  • You must be logged in to reply to this topic.