How do I go up a directory in php without using /.../...

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
johnny12358
Forum Newbie
Posts: 1
Joined: Sun Apr 25, 2010 2:50 pm

How do I go up a directory in php without using /.../...

Post by johnny12358 »

Hi, I am making a file browser in PHP, and I am struggling to go up a directory without using the /.../.

I don't like it because it makes all the file locations very long, and its just so annoying.

Can anybody please help??
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: How do I go up a directory in php without using /.../...

Post by Christopher »

Instead of $path . "/../" do dirname($path) . "/". Each time you call dirname() it will remove the last thing in the path -- either a file or dir name.
(#10850)
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: How do I go up a directory in php without using /.../...

Post by requinix »

Use /../, but if you find that, don't actually stick it into the path. Use dirname instead.

Code: Select all

if ($go_into == "..") {
    $path = dirname($path);
} else if ($go_into != ".") {
    $path = $path . "/" . $go_into;
}
Post Reply