File path problem.

Any questions involving matching text strings to patterns - the pattern is called a "regular expression."

Moderator: General Moderators

Post Reply
apg88
Forum Newbie
Posts: 16
Joined: Thu Aug 25, 2005 5:43 pm

File path problem.

Post by apg88 »

I'm developing a file manager with PHP. It get's the files and directories, prints them out on the page, and uses links to call itself again.
For example, It prints out this:
The directory is now ./

Code: Select all

./
../
.mozilla/
bin/
boot/
dev/
etc/
home/
lib/
If you click on etc/ it will change the directory to etc/ and print out this:
Directory is ./etc/

Code: Select all

./
../
4Suite/
X11/
acpi/
alchemist/
alsa/
alternatives/
ant.d/
bluetooth/
bonobo-activation/
cron.d/
cups/
dbus-1/
Now I click on X11/
The directory is now ./etc/X11/
I decide this is not the directory I want, so I click on ../ to go back one.
I am now in directory ./etc/X11/../
If I want to get out of etc, I click on ../ again.
The path is now ./ect/X11/../../
Now I want to go into the home directory:
The path to the directory is now ./ect/X11/../../home/

This pretty much gives you an idea of what I'm dealing with. After a while, your file path is full of ../ going in and out of directories jut to get to the one you want.
I think this is a waste of space and could be fixed easily with RegEx. But I'm not very good with it, so thats why I'm posting here. :-)

What I need to do is that if the user presses ../ the script runs a function that removes the last directory in the path string, instead of adding ../ to the end.
For example: If I'm in directory ./etc/X11/ and I press ../ I want it to change the path to ./etc/
The only thing the RegEx needs to do is remove the last directory in the string along with the '/' after it.

Thank's in advance ;-)
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

look into using realpath() with some string replacement maybe.
apg88
Forum Newbie
Posts: 16
Joined: Thu Aug 25, 2005 5:43 pm

Post by apg88 »

Awesome, It worked beautifully!
This is all I had to do:

Code: Select all

$this->path = realpath($d->path) . "/";
Post Reply