php file_get_contents 未找到存在的文件

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

file_get_contents is not finding a file that exists

phpfile-get-contents

提问by Rob

I have a file that I'd like another script to access using file_get_contents

我有一个文件,我希望使用另一个脚本访问 file_get_contents

The file I'd like it to access is in the directory above it, so I'm using file_get_contents('../file.php?this=that')

我希望它访问的文件在它上面的目录中,所以我正在使用 file_get_contents('../file.php?this=that')

However, it's returning No such file or directoryand I can't understand why. The file is there.

但是,它正在返回No such file or directory,我不明白为什么。文件在那里。

I'm assuming it has something to do with it being a local file rather than a remote. Any ideas or workarounds?

我假设它与它是本地文件而不是远程文件有关。任何想法或解决方法?

回答by Marc B

file_get_contents('../file.php?this=that')

This will never work, unless you build a full URL with the entire http://....syntax. PHP will see this as a request to get a file named file.php?this=thatone level above the current directory. It won't treat it as a relative URL and do an HTTP request, it'll use the local filesystem handler instead. You may very well have file.phpup there, but since the local file system has no concept of URLs or query parameters, it won't know to strip off the ?this=thatand just load file.php. Hence your 'no such file error'.

这永远不会奏效,除非您使用完整的http://....语法构建完整的 URL 。PHP 会将此视为获取名为file.php?this=that当前目录上一级的文件的请求。它不会将其视为相对 URL 并执行 HTTP 请求,而是使用本地文件系统处理程序。你很可能有file.php在那里,但由于本地文件系统没有网址或查询参数的概念,它将不知道脱光了?this=that,只是负载file.php。因此你的“没有这样的文件错误”。

回答by Schahriar SaffarShargh

According to PHP.net the correct solution to reading files using file_get_contents function from the local server is using

根据 PHP.net 使用 file_get_contents 函数从本地服务器读取文件的正确解决方案正在使用

// <= PHP 5
$file = file_get_contents('./people.txt', true);

// > PHP 5
$file = file_get_contents('./people.txt', FILE_USE_INCLUDE_PATH);

Thought it might help instead of using workarounds!

认为它可能会有所帮助而不是使用变通方法!

回答by Your Common Sense

There is no file in that location.
You have to use proper path.

该位置没有文件。
你必须使用正确的路径。

First of all do a echo getcwd();to see what directory is current now (from which your relative path is built)
Then double check file.php location in relation to this directory.
Check filename case, it could be sensitive.

首先echo getcwd();查看当前当前目录(从中构建您的相对路径)
然后仔细检查 file.php 相对于该目录的位置。
检查文件名大小写,它可能是敏感的。

May I ask a reason why do you open a php file with this function?

请问你为什么用这个功能打开一个php文件?

Well, an answer:

嗯,一个答案:

$your_var = 1;
include('../file.php');

回答by Rob

I went ahead and used a few $_SERVER variables, combining them together to get the full URL and used it in file_get_contents:

我继续使用了一些 $_SERVER 变量,将它们组合在一起以获得完整的 URL 并将其用于file_get_contents

file_get_contents('http://' . $_SERVER['HTTP_HOST'] . dirname($_SERVER['SCRIPT_NAME']) . '/../file.php?this=that');

file_get_contents('http://' . $_SERVER['HTTP_HOST'] . dirname($_SERVER['SCRIPT_NAME']) . '/../file.php?this=that');

That did the trick. Thanks for the help everyone.

这就是诀窍。谢谢大家的帮助。

回答by User1988

use like this....

像这样使用....

$string1=file_get_contents("c:/rose1/ram.txt"); echo $string1;

or $lines = file_get_contents('http://www.example.com/');

或者 $lines = file_get_contents('http://www.example.com/');

回答by Yanick Rochon

is getcwd() == dirname(__FILE__)?

getcwd() == dirname(__FILE__)

Once, I have encountered a problem where using a relative path was always throwing an error on some shared host. We ended up using absolute paths using dirname(__FILE__)as base path (setting a constant in the bootstrap and using that constant as base path value) and everything worked fine. We didn't dig further into the problem, but maybe you are encountering the same thing. I'm just guessing here.

有一次,我遇到了一个问题,使用相对路径总是在某些共享主机上抛出错误。我们最终使用绝对路径dirname(__FILE__)作为基本路径(在引导程序中设置一个常量并将该常量用作基本路径值)并且一切正常。我们没有深入研究这个问题,但也许您遇到了同样的事情。我只是在这里猜测。