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.
我有一个来自数据库的字符串,它是'. /Folder-B/image1.jpg',它指向的是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
在Folder-A的脚本中,我使用dirname(文件)来获取文件名,我得到mydomain.com/Folder-A。在这个脚本中,我需要得到一个字符串,它的名称是“mydomain.com/folder - b/image1 .jpg”。我试着
$path=dirname(__FILE__).'/'.'../Folder-B/image1.jpg';
This shows up as mydomain.com%2FFolder-A%2F..%2FFolder-B%2Fimage1.jpg
这显示为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?
这是一个facebook的分享按钮,它不能获取正确的图片。有人知道如何正确地找到路径吗?
Edit: I hope to get a url >>>mydomain.com%2FFolder-B%2Fimage1.jpg
编辑:我希望得到一个url >>>mydomain.com%2FFolder-B%2Fimage1.jpg。
7 个解决方案
#1
122
For PHP < 5.3 use:
对于PHP < 5.3使用:
$upOne = realpath(dirname(__FILE__) . '/..');
Or in PHP 5.3+ use:
或在PHP 5.3+中使用:
$upOne = realpath(__DIR__ . '/..');
#2
13
Try this
试试这个
dirname(dirname( __ FILE__))
Edit: removed "./" because it isn't correct syntax. Without it, it works perfectly.
编辑:删除”。/因为它不是正确的语法。没有它,它就能完美地工作。
#3
10
If you happen to have php 7.0 you could use levels.
如果您碰巧有php 7.0,您可以使用级别。
dirname( __FILE__, 2 )
with the second parameter you can define the amount of levels you want to go back.
dirname(__FILE__, 2)和第二个参数,您可以定义要返回的级别的数量。
http://php.net/manual/en/function.dirname.php
http://php.net/manual/en/function.dirname.php
#4
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.
可以使用PHP的dirname函数。< ?php echo目录名(__DIR__);? >。这将给出存储当前目录的__DIR__的父目录的名称。
#5
1
You can use realpath
to remove unnessesary part:
您可以使用realpath删除unnessesary部分:
// 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.
在windows上也用/如果需要的话在URL中替换\。
#6
0
One level up, I have used:
有一个层次,我用过:
str_replace(basename(__DIR__) . '/' . basename(__FILE__), '', realpath(__FILE__)) . '/required.php';
or for php < 5.3:
或者php < 5.3:
str_replace(basename(dirname(__FILE__)) . '/' . basename(__FILE__), '', realpath(__FILE__)) . '/required.php';
#7
-3
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'>");
#1
122
For PHP < 5.3 use:
对于PHP < 5.3使用:
$upOne = realpath(dirname(__FILE__) . '/..');
Or in PHP 5.3+ use:
或在PHP 5.3+中使用:
$upOne = realpath(__DIR__ . '/..');
#2
13
Try this
试试这个
dirname(dirname( __ FILE__))
Edit: removed "./" because it isn't correct syntax. Without it, it works perfectly.
编辑:删除”。/因为它不是正确的语法。没有它,它就能完美地工作。
#3
10
If you happen to have php 7.0 you could use levels.
如果您碰巧有php 7.0,您可以使用级别。
dirname( __FILE__, 2 )
with the second parameter you can define the amount of levels you want to go back.
dirname(__FILE__, 2)和第二个参数,您可以定义要返回的级别的数量。
http://php.net/manual/en/function.dirname.php
http://php.net/manual/en/function.dirname.php
#4
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.
可以使用PHP的dirname函数。< ?php echo目录名(__DIR__);? >。这将给出存储当前目录的__DIR__的父目录的名称。
#5
1
You can use realpath
to remove unnessesary part:
您可以使用realpath删除unnessesary部分:
// 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.
在windows上也用/如果需要的话在URL中替换\。
#6
0
One level up, I have used:
有一个层次,我用过:
str_replace(basename(__DIR__) . '/' . basename(__FILE__), '', realpath(__FILE__)) . '/required.php';
or for php < 5.3:
或者php < 5.3:
str_replace(basename(dirname(__FILE__)) . '/' . basename(__FILE__), '', realpath(__FILE__)) . '/required.php';
#7
-3
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'>");