Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get folder up one level

Tags:

php

I am using this:

echo dirname(__FILE__); 

which gives:

C:\UwAmp\www\myfolder\admin 

However I am looking for path until:

C:\UwAmp\www\myfolder\ 

from current script. How can that be done ?

like image 903
Dev555 Avatar asked Feb 05 '12 13:02

Dev555


People also ask

How do I move up one directory in PHP?

If you are using PHP 7.0 and above then the best way to navigate from your current directory or file path is to use dirname(). This function will return the parent directory of whatever path we pass to it.

What is __ DIR __ in PHP?

The __DIR__ can be used to obtain the current code working directory. It has been introduced in PHP beginning from version 5.3. It is similar to using dirname(__FILE__). Usually, it is used to include other files that is present in an included file.

How do I go back in dirname?

To go back 1 folder level with __dirname in Node. js, we can use the path. join method. const reqPath = path.


2 Answers

You could do either:

dirname(__DIR__); 

Or:

__DIR__ . '/..'; 

...but in a web server environment you will probably find that you are already working from current file's working directory, so you can probably just use:

'../' 

...to reference the directory above. You can replace __DIR__ with dirname(__FILE__) before PHP 5.3.0.

You should also be aware what __DIR__ and __FILE__ refers to:

The full path and filename of the file. If used inside an include, the name of the included file is returned.

So it may not always point to where you want it to.

like image 98
DaveRandom Avatar answered Oct 01 '22 08:10

DaveRandom


You can try

echo realpath(__DIR__ . DIRECTORY_SEPARATOR . '..');  
like image 21
Dan Soap Avatar answered Oct 01 '22 08:10

Dan Soap