php 为什么 file_get_contents 不起作用?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6724467/
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
Why doesn't file_get_contents work?
提问by PVJAZZ
Why does file_get_contents
notwork for me? In the test file code below, it seems that everyone's examples that I've searched for all have this function listed, but it never gets executed. Is this a problem with the web hosting service? Can someone test this code on their server just to see if the geocoding array output actually gets printed out as a string? Of course, I am trying to assign the output to a variable, but there is no output here in this test file....
为什么file_get_contents
不为我工作?在下面的测试文件代码中,似乎我搜索过的每个人的示例都列出了此函数,但从未执行过。这是网络托管服务的问题吗?有人可以在他们的服务器上测试这段代码,看看地理编码数组输出是否真的被打印为字符串?当然,我试图将输出分配给一个变量,但是这个测试文件中没有输出......
<html>
<head>
<title>Test File</title>
<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false">
</script>
</head>
<body>
<?
$adr = 'Sydney+NSW';
echo $adr;
$url = "http://maps.googleapis.com/maps/api/geocode/json?address=$adr&sensor=false";
echo '<p>'.$url.'</p>';
echo file_get_contents($url);
print '<p>'.file_get_contents($url).'</p>';
$jsonData = file_get_contents($url);
echo $jsonData;
?>
</body>
</html>
回答by hakre
Check file_get_contents
PHP Manualreturn value. If the value is FALSE
then it could not read the file. If the value is NULL
then the function itself is disabled.
检查file_get_contents
PHP 手册返回值。如果值为 ,FALSE
则无法读取文件。如果值为 ,NULL
则函数本身被禁用。
To learn more what might gone wrong with the file_get_contents
operation you must enable error reporting and the display of errors to actually read them.
要了解file_get_contents
操作中可能出现的问题的更多信息,您必须启用错误报告和错误显示才能实际读取它们。
# Enable Error Reporting and Display:
error_reporting(~0);
ini_set('display_errors', 1);
You can get more details about the why the call is failing by checking the INI values on your server. One value the directly effects the file_get_contents
function is allow_url_fopen
. You can do this by running the following code. You should note, that if it reports that fopen is not allowed, then you'll have to ask your provider to change this setting on your server in order for any code that require this function to work with URLs.
通过检查服务器上的 INI 值,您可以获得有关调用失败原因的更多详细信息。直接影响file_get_contents
函数的一个值是 allow_url_fopen
。您可以通过运行以下代码来做到这一点。您应该注意,如果它报告不允许 fopen,那么您必须要求您的提供商更改您服务器上的此设置,以便任何需要此功能的代码与 URL 一起使用。
<html>
<head>
<title>Test File</title>
<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false">
</script>
</head>
<body>
<?php
# Enable Error Reporting and Display:
error_reporting(~0);
ini_set('display_errors', 1);
$adr = 'Sydney+NSW';
echo $adr;
$url = "http://maps.googleapis.com/maps/api/geocode/json?address=$adr&sensor=false";
echo '<p>'.$url.'</p>';
$jsonData = file_get_contents($url);
print '<p>', var_dump($jsonData), '</p>';
# Output information about allow_url_fopen:
if (ini_get('allow_url_fopen') == 1) {
echo '<p style="color: #0A0;">fopen is allowed on this host.</p>';
} else {
echo '<p style="color: #A00;">fopen is not allowed on this host.</p>';
}
# Decide what to do based on return value:
if ($jsonData === FALSE) {
echo "Failed to open the URL ", htmlspecialchars($url);
} elseif ($jsonData === NULL) {
echo "Function is disabled.";
} else {
echo $jsonData;
}
?>
</body>
</html>
If all of this fails, it might be due to the use of short open tags, <?
. The example code in this answer has been therefore changed to make use of <?php
to work correctly as this is guaranteed to work on in all version of PHP, no matter what configuration options are set. To do so for your own script, just replace <?
or <?php
.
如果所有这些都失败了,则可能是由于使用了短的开放标签<?
. 因此,此答案中的示例代码已更改以使其<?php
正常工作,因为无论设置什么配置选项,都可以保证在所有版本的 PHP 中都可以使用。要为您自己的脚本执行此操作,只需替换<?
或<?php
。
回答by Nick F
If PHP's allow_url_fopen
ini directive is set to true, and if curl
doesn't work either (see this answerfor an example of how to use it instead of file_get_contents
), then the problem could be that your server has a firewall preventing scripts from getting the contents of arbitrary urls (which could potentially allow malicious code to fetch things).
如果 PHP 的allow_url_fopen
ini 指令设置为 true,并且curl
也不起作用(有关如何使用它而不是 的示例,请参阅此答案file_get_contents
),则问题可能是您的服务器有防火墙阻止脚本获取内容任意网址(可能允许恶意代码获取内容)。
I had this problem, and found that the solution for me was to edit the firewall settings to explicitly allow requests to the domain (or IP address) in question.
我遇到了这个问题,并发现我的解决方案是编辑防火墙设置以明确允许对相关域(或 IP 地址)的请求。
回答by Stefan Gruenwald
If it is a local file, you have to wrap it in htmlspecialchars like so:
如果它是本地文件,则必须将其包装在 htmlspecialchars 中,如下所示:
$myfile = htmlspecialchars(file_get_contents($file_name));
Then it works
然后它工作
回答by Michael J. Calkins
Wrap your $adr
in urlencode()
.
I was having this problem and this solved it for me.
把你$adr
的urlencode()
. 我遇到了这个问题,这为我解决了。
回答by Rameez SOOMRO
//JUST ADD urlencode();
$url = urlencode("http://maps.googleapis.com/maps/api/geocode/json?address=$adr&sensor=false");
<html>
<head>
<title>Test File</title>
<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false">
</script>
</head>
<body>
<?php
$adr = 'Sydney+NSW';
echo $adr;
$url = "http://maps.googleapis.com/maps/api/geocode/json?address=$adr&sensor=false";
echo '<p>'.$url.'</p>';
echo file_get_contents($url);
print '<p>'.file_get_contents($url).'</p>';
$jsonData = file_get_contents($url);
echo $jsonData;
?>
</body>
</html>