...

View Full Version : Echo Current Directory



Troy297
08-07-2007, 07:53 PM
Hey,

Im just wondering how to echo the current directory of something without getting the whole url (/home/user/public_html/folder/). So basically if it is in my forum directory I just want to get "/forum/" without the full document root.

Anyone know how? I tried REQUEST_URI but the problem is it echoes the whole url (include the filename).

Thanks!

rafiki
08-07-2007, 08:04 PM
explode the REQUEST_URI to get the current directory

Troy297
08-07-2007, 08:05 PM
Okay... but say the current url is (/staff/panel/usercp.php) how do I manage that if the "usercp.php" could end up being different every time the page reloads?

rafiki
08-07-2007, 08:09 PM
$url = $_SERVER['REQUEST_URI']; //returns URL
$parts = explode('/',$url);
print_r($parts);

that should help you work things out

Troy297
08-07-2007, 08:32 PM
Alright... I'm lost now...

I understand how the whole array thing works but I can't seem to figure out preg_replace() or preg_match() which I assume you would have to use to remove the file part.

Sorry... :) anyone provide me with a working example?

Troy297
08-07-2007, 09:10 PM
Scratch that, I've almost got it :)


$url = $_SERVER['REQUEST_URI'];
$parts = explode('/',$url);
print_r($parts);
echo "<p>";
if(preg_match('/.php/', $parts[2])){
$final = preg_replace('/(.*)(.php)(.*)/','',$parts[2]);
echo "/".$parts[0].$parts[1].$final."/";
}

How can I get the preg_match to cycle through the whole array without writing it all out individually?

matak
08-07-2007, 09:28 PM
how to echo the current directory of something without getting the whole url (/home/user/public_html/folder/).

maybe getcwd(); (http://php.net/getcwd)

Troy297
08-07-2007, 10:43 PM
Thxs for the suggestion but I've already tried, and I've finally perfected it, I think?


$url = $_SERVER['REQUEST_URI'];
$parts = explode('/',$url);
foreach($parts as $pa){
if(preg_match('/(.php)/',$pa)){
$final = preg_replace('/(.*)(.php)(.*)/', '', $pa);
echo $final."/";
}else{
echo $pa;
}
}

Now the only thing is how can I piece everything back together into a viable location/folder dir? I need it in the form of a php variable.

rafiki
08-07-2007, 10:46 PM
''$pa.''$ps;
??

Troy297
08-07-2007, 10:49 PM
Well the problem I now have is if I modify the code slightly to look like this:


$url = $_SERVER['REQUEST_URI'];
$parts = explode('/',$url);
foreach($parts as $pa){
if(preg_match('/(.php)/',$pa)){
$final = preg_replace('/(.*)(.php)(.*)/', '', $pa);
$end = $final;
}else{
$dir = $pa;
}
}
$folder = "/".$dir.$end;
echo $folder;

It seems logical enough but what happens when the location is inside multiple folders, like this (forum/staff/install/)? How can I do that so that $dir won't just overwrite itself every time and then piece it all together at the end...?

Thanks!

Troy297
08-07-2007, 11:17 PM
Anyone help please?

firepages
08-08-2007, 01:41 AM
you should have stuck with where rafiki was going which was going to be far easier.

this will work on *nix, you would need to modify for win32


<?
$bits = explode('/', $_SERVER['SCRIPT_FILENAME']);
echo $bits[(count($bits)-2)];
?>
& for the full path...
<?
$path = str_replace(basename($_SERVER['SCRIPT_FILENAME']),'', $_SERVER['SCRIPT_FILENAME']);
echo $path;
?>

Troy297
08-08-2007, 04:37 AM
Ya... that is alot easier - problem was I didn't know where rafiki was going ;)

Anyways... one last thing.... the first code snippet works great for what I need until you run into sub-level directories (mysite.com/v2/panel/staff/) - the code only shows the last one... not everything leading up to...

I'll look up the count in the php manual to see if I can figure something out but does anyone know the "simple" fix?

Thanks :)

usik
08-08-2007, 08:05 AM
Here is an alternative I just came up with :p


<?php
class dir {
function dir() {
$location = $_SERVER["SCRIPT_NAME"];
list($this->dir) = split("[a-zA-Z0-9]*.php$", $location, 2);
}
}
$directory = new dir();
echo $directory->dir . "<br />";
?>


basically it uses regular expressions to find a part in the script file path name that can consist of many letters and digits ending with .php and is at the end of the string, thus deleting the script name from the end of the string and you are left with the current directory. if u hav underscores or hyphens in ur scripts than add split("[a-zA-Z0-9_\-]*.php$", $location, 2) or it might be a front slash I cant remember what the cancellation symbol is for regex.
Hope this has helped!
(iv edited this post a couple of times because i had some problems with the code, like deleting a whole directory from the string if on of the folders was called php, but this script has been tested and works well now)

Troy297
08-08-2007, 09:09 PM
Wow! Thanks so much!!!

It works like a charm and does exactly what I wanted it to!!!

Thanks again!! +Rep!



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum