100

I have a folder structure as follows:

mydomain.example
  ->Folder-A
  ->Folder-B

I have a string from Database that is ../Folder-B/image1.jpg, which points to an image in Folder-B.

Inside a script in Folder-A, I am using dirname(__FILE__) to fetch the filename and I get mydomain.example/Folder-A. Inside this script, I need to get a string that says 'mydomain.example/Folder-B/image1.jpg. I tried

$path=dirname(__FILE__).'/'.'../Folder-B/image1.jpg';

This shows up as mydomain.com%2FFolder-A%2F..%2FFolder-B%2Fimage1.jpg

This is for a Facebook share button, and this fails to fetch the correct image. Anyone know how to get the path correctly?

I hope to get a URL: mydomain.example%2FFolder-B%2Fimage1.jpg

1
  • This answer by Petah actually answers the question better than the accepted answer. (It actually tells you how to move up a level, not just how to avoid having to.)
    – Byson
    Dec 12, 2014 at 11:44

9 Answers 9

296

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);
5
  • The problem is , My target file is saved as ../Folder-B/image1.jpg in the database. So with your approach as well as mine, I get mydomain.com/Folder-A/../Folder-B/image1.jpg. The share button does not recognize it, though If I copy this to a browser, it seems to fetch the correct image.
    – aVC
    Jun 19, 2012 at 5:27
  • 1
    @Petah - I've spent hours on this, then discovered realpath() from your post. Resolved my last niggle with the output perfectly, thank you!
    – James
    Sep 12, 2013 at 1:04
  • 1
    This answer actually answers the question. The accepted answer just provides a workaround to avoid the problem, but does not actually tell us "how to go one level up on dirname(FILE)". Well done.
    – Byson
    Dec 12, 2014 at 11:42
  • 1
    Alternative way: dirname(__DIR__)
    – Nefelim
    Jan 10, 2018 at 8:45
  • @Nefelim yea, that is mentioned in the other answers.
    – Petah
    Jan 11, 2018 at 3:19
24

If you happen to have php 7.0+ you could use levels.

dirname( __FILE__, 2 ) with the second parameter you can define the amount of levels you want to go back.

http://php.net/manual/en/function.dirname.php

18

Try this

dirname(dirname( __FILE__ ))

Edit: removed "./" because it isn't correct syntax. Without it, it works perfectly.

6

You could use PHP's dirname function. <?php echo dirname(__DIR__); ?>. That will give you the name of the parent directory of __DIR__, which stores the current directory.

5
  • I tried that, and I can get the url. the problem is, I have a ../ in my target file's name that is saved in the database. I need to get rid of that in the final url.
    – aVC
    Jun 19, 2012 at 5:30
  • Maybe a regular expression, something along the lines of: /[^\/\]+\.\.\/?\/ Use that to remove the parent/../ from the path. 'should' work, have not tested however.
    – Shane
    Jun 19, 2012 at 5:44
  • Sorry, I made a slight error in the above regular expression, ?\/ should actually be ?/
    – Shane
    Jun 19, 2012 at 5:50
  • No problem, I figured it was a typo. I did figure out the problem with some additional reading. Thanks for your comments :)
    – aVC
    Jun 19, 2012 at 6:28
  • Best solution IMHO
    – Nefelim
    Jan 10, 2018 at 8:46
5
dirname(__DIR__,level);
dirname(__DIR__,1);

level is how many times will you go back to the folder

1

You can use realpath to remove unnessesary part:

// One level up
echo str_replace(realpath(dirname(__FILE__) . '/..'), '', realpath(dirname(__FILE__)));

// Two levels etc.
echo str_replace(realpath(dirname(__FILE__) . '/../..'), '', realpath(dirname(__FILE__)));

On windows also replace \ with / if need that in URL.

0

One level up, I have used:

str_replace(basename(__DIR__) . '/' . basename(__FILE__), '', realpath(__FILE__)) . '/required.php';

or for php < 5.3:

str_replace(basename(dirname(__FILE__)) . '/' . basename(__FILE__), '', realpath(__FILE__)) . '/required.php';
0

To Whom, deailing with share hosting environment and still chance to have Current PHP less than 7.0 Who does not have dirname( __FILE__, 2 ); it is possible to use following.

function dirname_safe($path, $level = 0){
    $dir = explode(DIRECTORY_SEPARATOR, $path);
    $level = $level * -1;
    if($level == 0) $level = count($dir);
    array_splice($dir, $level);
    return implode($dir, DIRECTORY_SEPARATOR).DIRECTORY_SEPARATOR;
}

print_r(dirname_safe(__DIR__, 2));
-2

I use this, if there is an absolute path (this is an example):

$img = imagecreatefromjpeg($_SERVER['DOCUMENT_ROOT']."/Folder-B/image1.jpg");

if there is a picture to show, this is enough:

echo("<img src='/Folder-B/image1.jpg'>");

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.