Reputation: 10139
If my website is stored on the server somewhere like this:
/home/usr1/public_html/
Which maps to:
http://example.com/
But then I am in a file at this web address:
http://example.com/section1/page1.php
And I want to include a file held at:
/home/usr1/public_html/includes/common.php
Is there a simple function in PHP I can use to compare one path against another (eg. the current path) to get the relative string required to get at one from the other?
Eg:
$thisPath = dirname(__FILE__);
// $thisPath is now equal to:
// /home/usr1/public_html/section1/page1.php
$relPath = comparePaths($thisPath, '/home/usr1/public_html/includes/common.php');
// $relPath is now equal to:
// ../includes/common.php
$relPath2 = comparePaths($thisPath, '/home/usr1/public_html/section1/part2/anotherpage.php');
// $relPath2 is now equal to:
// part2/anotherpage.php
Does such a feature or function (or similar) exist?
Upvotes: 0
Views: 986
Reputation: 166
If you want to do an include and already have the absolute path, I'd recommend to use it and don't care about relative paths. Trying to figure out a relative path for a local file inclusion just adds more steps and more possibility for failure to the process. Or is there any reason why you need a relative path that I'm missing?
If you don't know the complete absolute path, but the relative path to your project's base folder, save the absolute path of your project's base folder once and then concatenate the relative path to your file.
I'd also recommend not to put your includes inside the public HTML folder. For security reasons you'd rather want them to reside in a folder that's not accessible via HTTP to prevent possible source code exposure or CSRF in case of webserver misconfiguration.
/home/usr1/includes/common.php
This way the file would be only accessible for local include or other file operations since your webserver won't serve the file like this
http://example.com/../includes/common.php
Upvotes: 1
Reputation: 54
In real you don't need for get abs path like this
/home/usr1/public_html/includes/common.php
In First you need for all requests to your domain send to one file (for example name controller.php) and that file is located in your root direction:
/home/usr1/public_html/controller.php
For send requests to one file:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ controller.php?rout=$1 [QSA,L]
server {
.
.
// Here your
.
.
location / {
if (!-e $request_filename){
rewrite ^(.*)$ /controller.php?rout=$1 break;
}
}
}
After need for in that controller.php create home url (root with your domain):
$link = "http" . ( isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? "s" : null ) . "://" . $_SERVER['HTTP_HOST'];
//$link - link is your domain
// $link == http://example.com/
After In your controller.php you have an all requests after your domain
// if request == http://example.com/blabla
echo $_GET['rout']; // '/blabla'
// if request == http://example.com/blabla/dabladabla
echo $_GET['rout']; // '/blabla/dabladabla'
After depended from your $_GET['rout']; you can controll which file can include Your example (controller.php):
include "includes/common.php";
Upvotes: 1