views:

56

answers:

4

Hello,

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!

+1  A: 

Take a look at the dirname() function.

From the documents, dirname() removes the trailing slash. If you want to keep it you can append the constant DIRECTORY_SEPARATOR to the result.

$dir = dirname('mystring/and/path.txt').DIRECTORY_SEPARATOR;
Yacoby
+1  A: 
$page_directory = dirname($page_file_temp);

See dirname.

David
Got it :) thank you! I was on the wrong path..
Adrian M.
A: 

An alternative:

$directory = pathinfo($page_file_temp,PATHINFO_DIRNAME);

http://www.php.net/manual/en/function.pathinfo.php

deadkarma
A: 

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);
Dominic Barnes