Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
482 views
Welcome To Ask or Share your Answers For Others

1 Answer

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);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...