The Way to Programming
The Way to Programming
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?
PHP:
require_once dirname(__FILE__).'/includes/config.inc.php';
HTML:
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?
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
http://example.com/home/user/public_html/includes/config.php
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.
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.
You could refer to the base url of your website instead of an absolute directory
Sign in to your account