View Full Version : Identifying The Name Of A Folder

09-18-2003, 08:34 AM
One one site I am using this script to identify the name of the file that executes that script (minus the extension)

preg_replace('/(.+)(\..+?)$/', '\\1', basename($_SERVER['PHP_SELF']))

On a new site I want to identify the name of the folder containing the file executs this script, ie. if this code was run from www.mysite.com/folder/file.php at the moment it will return "file" but I would like it to return "folder".

I am not sure where I got the above script but I am absolutely dumbfounded when it comes to regular expressions! I would really appreciate any help I can get on making this folder identifying script.

Thanks heaps.

09-18-2003, 11:25 AM
I'd go for exploding the variable around a / delimiter and then accessing the array parts

$path_bits = explode('/',basename($_SERVER['PHP_SELF']);
$num_bits = count($path_bits);

$file = $path_bits[($num_bits - 1)];
$folder = $path_bits[($num_bits - 2)];

09-18-2003, 03:31 PM
Yeah, that's much easier to follow!

We have to remove the word "basename" but appart from that it works perfectly.

Thanks a lot :thumbsup:

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum