Reputation: 7443
I have:
$page_file_temp = $_SERVER["PHP_SELF"];
which will output: /templates/somename/index.php
I want to extract from that path only "/templates/somename/"
How can I do it? Thanks!
Upvotes: 16
Views: 67869
Reputation: 356
Maybe this is your solution:
$rootPath = $_SERVER['DOCUMENT_ROOT'];
$thisPath = dirname($_SERVER['PHP_SELF']);
$onlyPath = str_replace($rootPath, '', $thisPath);
For example:
$_SERVER['DOCUMENT_ROOT']
is the server's root-path like this /home/abc/domains/abc.com/public_html
$_SERVER['PHP_SELF']
is about the whole path to that script like this /home/abc/domains/abc.com/public_html/uploads/home/process.php
Then we can have:
$rootPath
like this /home/abc/domains/abc.com/public_html
$thisPath
like this /home/abc/domains/abc.com/public_html/uploads/home
And $onlyPath
like this /uploads/home
Upvotes: 10
Reputation: 28439
Using parse_url will account for GET variables and "fragments" (portion of URL after #) amongst other URL-specific parts.
$url = $_SERVER['PHP_SELF']; // OR $_SERVER['REQUEST_URI']
echo parse_url($url, PHP_URL_PATH);
Upvotes: 1
Reputation: 3144
An alternative:
$directory = pathinfo($page_file_temp,PATHINFO_DIRNAME);
http://www.php.net/manual/en/function.pathinfo.php
Upvotes: 0