PHP how to go one level up on dirname(__FILE__)
For PHP < 5.3 use:
$upOne = realpath(dirname(__FILE__) . '/..');
In PHP 5.3 to 5.6 use:
$upOne = realpath(__DIR__ . '/..');
In PHP >= 7.0 use:
$upOne = dirname(__DIR__, 1);
Php how to go one level down on dirname(__FILE__)?
Please use double time dirname() function to back one level
dirname(dirname(__FILE__));
Get folder up one level
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.
Get folder up one level
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.
Get 2 levels up from dirname( __FILE__)
PHP 5.3+
return dirname(__DIR__);
PHP 5.2 and lower
return dirname(dirname(__FILE__));
With PHP7 go further up the directory tree by specifying the 2nd argument to dirname
. Versions prior to 7 will require further nesting of dirname
.
http://php.net/manual/en/function.dirname.php
file_get_contents with relative path
Have you tried:
$json = file_get_contents(__DIR__ . '/../validate/edit.json');
__DIR__
is a useful magic constant.
For reasons why, see http://yagudaev.com/posts/resolving-php-relative-path-problem/.
When a PHP file includes another PHP file which itself includes yet another file — all being in separate directories — using relative paths to include them may raise a problem.
PHP will often report that it is unable to find the third file, but why?
Well the answer lies in the fact that when including files in PHP the interpreter tries to find the file in the current working directory.In other words, if you run the script in a directory called A and you include a script that is found in directory B, then the relative path will be resolved relative to A when executing a script found in directory B.
So, if the script inside directory B includes another file that is in a different directory, the path will still be calculated relative to A not relative to B as you might expect.
php how to go two level up on dirname(__FILE__)
You can use:
realpath(__DIR__ . "/..")
Related Topics
Easy Login Script Without Database
How to Connect User with a Login Cookie in PHP
Convert Epoch Time to Date PHP
Convert the First Element of an Array to a String in PHP
Oracle 11G Xe Greek Character Set Not Displaying
How to Write Laravel Groupby Query
What Is the Concept of Service Container in Laravel
Using Websocket on Apache Server
Changing the Add to Cart Button Text in Woocommerce for Items with Variations
Query Time Result in MySQL W/ PHP
Best Practice for Working with Currency Values in PHP