Impossible To Use Include Function When Resource Paths Are Relative
Solution 1:
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 include
s/require
s, 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
<ahref="/index.php">Home</a><ahref="/cabbages.php">My Collection of Giant Cabbages</a><ahref="/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><?phpinclude$_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.
Solution 2:
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.
Post a Comment for "Impossible To Use Include Function When Resource Paths Are Relative"