83

I have a folder structure as follows:

mydomain.com
  ->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.com/Folder-A. Inside this script, I need to get a string that says 'mydomain.com/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?

Edit: I hope to get a url >>>mydomain.com%2FFolder-B%2Fimage1.jpg

CBroe
  • 82,033
  • 9
  • 81
  • 132
aVC
  • 2,056
  • 2
  • 19
  • 44
  • [This answer by Petah](http://stackoverflow.com/questions/11094776/php-how-to-go-one-level-up-on-dirname-file#answer-11094813) 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 '14 at 11:44

9 Answers9

222

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);
Petah
  • 42,792
  • 26
  • 149
  • 203
  • 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 '12 at 05: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 '13 at 01: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 '14 at 11:42
  • 1
    Alternative way: `dirname(__DIR__)` – Nefelim Jan 10 '18 at 08:45
  • @Nefelim yea, that is mentioned in the other answers. – Petah Jan 11 '18 at 03:19
21

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

Bas van Dijk
  • 729
  • 6
  • 20
18

Try this

dirname(dirname( __ FILE__))

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

Abhishek
  • 681
  • 3
  • 14
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.

Shane
  • 1,806
  • 14
  • 31
  • 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 '12 at 05: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 '12 at 05:44
  • Sorry, I made a slight error in the above regular expression, ?\/ should actually be ?/ – Shane Jun 19 '12 at 05: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 '12 at 06:28
  • **Best solution** IMHO – Nefelim Jan 10 '18 at 08:46
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.

1
dirname(__DIR__,level);
dirname(__DIR__,1);

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

dılo sürücü
  • 1,989
  • 1
  • 12
  • 19
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';
Calin Rusu
  • 167
  • 1
  • 5
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));
Deniz Porsuk
  • 494
  • 1
  • 6
  • 17
-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'>");