php 如何在 dirname(__FILE__) 上上一级

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11094776/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 23:45:08  来源:igfitidea点击:

php how to go one level up on dirname(__FILE__)

phpdirname

提问by aVC

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( FILE) 来获取文件名,然后我得到 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

回答by Petah

For PHP < 5.3 use:

对于 PHP < 5.3 使用:

$upOne = realpath(dirname(__FILE__) . '/..');

In PHP 5.3 to 5.6 use:

在 PHP 5.3 到 5.6 中使用:

$upOne = realpath(__DIR__ . '/..');

In PHP >= 7.0 use:

在 PHP >= 7.0 中使用:

$upOne = dirname(__DIR__, 1);

回答by Bas van Dijk

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

回答by Abhishek

Try this

尝试这个

dirname(dirname( __ FILE__))

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

编辑:删除了“./”,因为它的语法不正确。没有它,它可以完美运行。

回答by Shane

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 dirname(__DIR__); ?>. 这将为您提供__DIR__存储当前目录的父目录的名称。

回答by Shane

You can use realpathto remove unnessesary part:

您可以使用realpath删除不必要的部分:

// 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。

回答by Calin Rusu

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

回答by Deniz Porsuk

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.

对于谁,处理共享托管环境并且仍然有机会拥有 Current PHP 低于 7.0 谁没有dirname( __FILE__, 2 );它可以使用以下。

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

回答by d?lo sürücü

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

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

级别是您将返回文件夹的次数

回答by d?lo sürücü

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