php file_get_contents 与相对路径

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29798629/
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-26 00:35:49  来源:igfitidea点击:

file_get_contents with relative path

php

提问by user1032531

I have the following directory structure.

我有以下目录结构。

/var/www/base/controller/detail.php
/var/www/base/validate/edit.json
/var/www/html

Within /var/www/base/controller/detail.php, how do I use file_get_contents()with a relative path to read /var/www/base/validate/edit.json? I've tried the following:

在里面/var/www/base/controller/detail.php,我如何使用file_get_contents()相对路径来读取/var/www/base/validate/edit.json?我尝试了以下方法:

//failed to open stream: No such file or directory (error no: 2)
$json=file_get_contents('detail.php');

//No error, but I don't want this file and was just testing
$json=file_get_contents('detail.php', FILE_USE_INCLUDE_PATH);

//failed to open stream: No such file or directory (error no: 2)
$json=file_get_contents('./validate/edit.json', FILE_USE_INCLUDE_PATH);
//failed to open stream: No such file or directory (error no: 2)
$json=file_get_contents('../validate/edit.json', FILE_USE_INCLUDE_PATH);
//failed to open stream: No such file or directory (error no: 2)
$json=file_get_contents('././validate/edit.json', FILE_USE_INCLUDE_PATH);
//failed to open stream: No such file or directory (error no: 2)
$json=file_get_contents('../../validate/edit.json', FILE_USE_INCLUDE_PATH);

//This works, but I want to use a relative path
$json=file_get_contents(dirname(dirname(__FILE__)).'/validate/edit.json');

回答by Samuel Parkinson

Have you tried:

你有没有尝试过:

$json = file_get_contents(__DIR__ . '/../validate/edit.json');

__DIR__is a useful magic constant.

__DIR__是一个有用的魔法常数。

For reasons why, see http://yagudaev.com/posts/resolving-php-relative-path-problem/.

原因请参见http://yagudaev.com/posts/resolving-php-relative-path-problem/

When a PHP file includes another PHP file which itself includes yet another file — all being in separate directories — using relative paths to include them may raise a problem.

PHP will often report that it is unable to find the third file, but why? Well the answer lies in the fact that when including files in PHP the interpreter tries to find the file in the current working directory.

In other words, if you run the script in a directory called A and you include a script that is found in directory B, then the relative path will be resolved relative to A when executing a script found in directory B.

So, if the script inside directory B includes another file that is in a different directory, the path will still be calculated relative to A not relative to B as you might expect.

当一个 PHP 文件包含另一个 PHP 文件时,该文件本身又包含另一个文件——所有文件都在单独的目录中——使用相对路径来包含它们可能会引发问题。

PHP经常会报找不到第三个文件,但是为什么呢?答案在于,当在 PHP 中包含文件时,解释器会尝试在当前工作目录中查找文件。

换句话说,如果您在名为 A 的目录中运行脚本并包含在目录 B 中找到的脚本,则在执行在目录 B 中找到的脚本时,相对路径将相对于 A 进行解析。

因此,如果目录 B 中的脚本包含位于不同目录中的另一个文件,则路径仍将相对于 A 计算,而不是像您预期的那样相对于 B。

回答by Hara Prasad

try using this

尝试使用这个

$json = file_get_contents("/path/to/your/file/edit.json", true);

As of PHP 5 the FILE_USE_INCLUDE_PATH constant can be used to trigger include path search. This is not possible if strict typing is enabled since FILE_USE_INCLUDE_PATH is an int. Use TRUE instead.

从 PHP 5 开始, FILE_USE_INCLUDE_PATH 常量可用于触发包含路径搜索。如果启用了严格类型,这是不可能的,因为 FILE_USE_INCLUDE_PATH 是一个 int。请改用 TRUE。

回答by Paragate

Of cause there is the possibility to use the include_path parameter. Set this parameter to '1' then a search for the file in the include_path (in php.ini.) will be done. You have to do editing in the php.ini!

当然有可能使用 include_path 参数。将此参数设置为“1”,然后将在 include_path(在 php.ini. 中)中搜索文件。你必须在php.ini中做编辑!

When using $json=file_get_contents('detail.php'); a call is done from a php file. Use file_get_contents('detail.php'); to have the detail.php get executed. the file have to be in the root directory (same as the calling php file in which the file_get_contents() is situated). If detail.php is in a subdirectory i can not see any workaround than using the include_path parameter

使用时 $json=file_get_contents('detail.php'); 调用是从 php 文件完成的。使用 file_get_contents('detail.php'); 执行 detail.php。该文件必须位于根目录中(与 file_get_contents() 所在的调用 php 文件相同)。如果 detail.php 在子目录中,除了使用 include_path 参数,我看不到任何解决方法

回答by helvete

You can always try to do the opposite first to find out:

你总是可以先尝试做相反的事情来找出:

file_put_contents('FINDME', 'smth');
exit();
$json=file_get_contents('detail.php');
...

Then run something like this from your terminal on *nix system (or search for a file named FINDMEusing GUI on Windows)

然后在 *nix 系统上从终端运行类似的程序(或FINDME在 Windows 上搜索使用 GUI命名的文件)

find <root-dir-of-the-project> -name 'FINDME'

The command will output something like this:

该命令将输出如下内容:

<root-dir-of-the-project>/<directories>/FINDME

Now you know the root dir(from which the relative path is being taken) for the file where you are attempting to read the other files and you can construct the relative path easily

现在您知道了您尝试读取其他文件的文件的根目录(从中获取相对路径),并且您可以轻松构建相对路径