Code with C | Programming: Projects & Source Codes › Forums › PHP › PHP – absolute file paths in HTML
November 28, 2016 at 3:55 pm #11177
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?
HTML:123<div><a href=<?php dirname(__FILE__)? rel="nofollow">"/includes/config.inc.php">View Files</a></div>
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
Also, are the backslashes for a new folder performed in the PHP or the HTML?
November 28, 2016 at 3:56 pm #11178
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.
November 28, 2016 at 3:56 pm #11179
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.
November 28, 2016 at 3:56 pm #11180
You could refer to the base url of your website instead of an absolute directory12345678< ?php$pageURL = (@$_SERVER["HTTPS"] == "on") ? "https://" : "http://";$pageURL .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];?><div><a href=<?php echo $pageURL; ? rel="nofollow">"/includes/config.inc.php">View Files</a></div>
You must be logged in to reply to this topic.