user5877383
user5877383

Reputation:

Impossible to use include function when resource paths are relative

Background: I'm currently very new to PHP and generating content dynamically, I'm using PHPStorm and using XAMPP's PHP interpreter combined with PHPStorm's built-in web server.

Problem: I've recently learnt the very handy function include() with this I am now dynamically including my: head content, navigation and footer. This works well when I'm doing it in the index.php in the root directory, because I use relative paths for things like images in the navigation, and script paths.

But when I create a folder for a new page (directory) for example 'article' and then I create an index.php in there that uses the same include functions, the resources that were using relative paths won't be able to source their files because I am now 1 directory deep and the relative paths are specified from within the files I am including?

For example in my navigation.html I have <img alt="Logo" title="Logo" src="img/brand/Logo.png"> This will work in the root directory of the server because the path is correct but one included within documents further down in folders it will not.

Can someone explain the best practice when working with including navigation, the only thing I can think of is to duplicate the libraries I'm including and make a set with relative paths that go back +1 directories for use on the pages that have to be within folders

Upvotes: 2

Views: 1504

Answers (2)

Martin
Martin

Reputation: 22760

PHP uses paths relative to your local server machine, while HTML uses paths relative to your website. There is fundamental difference as to how each of these are defined at run time.

PHP

If you are using PHP you should get into a habit of NOT using relative paths at all but to use an absolute path, which will guarentee to succeed every time (As long as the target file exists and is reachable, etc.).

so; use $_SERVER['DOCUMENT_ROOT']

As a side note, you do not need to use brackets for your includes/requires, it's simply giving the server more work to do for no extra benefit.

The $_SERVER['DOCUMENT_ROOT'] is the base directory of your PHP/web application, typically the contents of the folder public_html/.

HTML

HTML also uses document rooting, but has a easier way of referencing them tan typing out a PHP $_SERVER variable each time.

When referencing other files/media in your HTML, you should avoid document-relative paths, instead set each path as being website root relative, this is easily accomplished with starting each file/image request with a slash (/) which indicates the browser should start looking from the base HTML directory.

File: logo.png
location: www.site.com/images/logo.php

Reaching the file from www.site.com/greatholidays/index.php would mean you would need ../images/logo.png but to include some HTML which references this file (such as a header include) in PHP would mean that this call would not work on the www.site.com/index.php page; so instead simply preceed each reference call with a slash:

../images/logo.png ==> Becomes ==> /images/logo.png
                                 ^^^
                       Tells the browser to look from the base directory. 

Example:

  • Included file: /public_html/inc/nav.php

     <a href="/index.php">Home</a>
     <a href="/cabbages.php">My Collection of Giant Cabbages</a>
     <a href="/horse/whyIlovehorses.php">Why I love horses</a>
    
  • File include is called from: www.site.com/index.php ~ /public_html/index.php

     <html>
         <body>
         <main>
             <nav>
             <?php include $_SERVER['DOCUMENT_ROOT']."/inc/nav.php"; ?>
            </nav>
         </main>
        ...
    

    In the above example, it does not matter where the nav.php file is called from by PHP or where the HTML output is displayed from, the referenced links in the nav element will always reach the same places.

Upvotes: 1

Justin
Justin

Reputation: 16

The simplest way to achieve this is with a root-relative path. If you begin every link with a "/" it is read as relative to the root directory. Therefore on mywebsite.com, a link to "/folder/file.php" will always go to "mywebsite.com/folder/file.php" regardless of how many folders deep the current page is.

This works great for live sites on a server. The problem I'm struggling to work out is it fails in my local XAMPP setup (my testing server) and on a shared hosting setup where the domain name has not gone live yet because in both cases the server maps the root directory to the wrong place but on a live site, it solves your problem perfectly.

Upvotes: 0

Related Questions