php - 即使 allow_url_fopen = On,file_get_contents() 也会返回空
问题描述
问题
file_get_contents()
由于某种原因返回一个空字符串。
代码
索引.php
<?php
$value = file_get_contents("http://foo.com/somefile.txt");
echo $value;
?>
php.ini
allow_url_fopen = On
allow_url_include = On
http://foo.com/somefile.txt
69.00000000
我的研究
通常当file_get_contents("http://foo.com/somefile.txt")
返回一个空字符串时,它是由于两个原因之一
somefile.txt
是一个空文件php.ini
有allow_url_include = Off
$value
现在应该是69.00000000
,但该函数不返回任何内容。
问题
为什么$value
函数调用后为空?
解决方案
try this:
checkRemote("http://foo.com/somefile.txt");
function checkRemote($url)
{
if(!checkRemoteLink($url)){
echo 'bad link!';
} else if(!checkRemoteFile($url)){
echo 'bad file!';
} else echo 'trouble!';
}
function checkRemoteLink($url)
{
$file_headers = @get_headers($url);
if (!$file_headers || $file_headers[0] == 'HTTP/1.1 404 Not Found') {
return false;
} else {
return true;
}
}
function checkRemoteFile($url)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_NOBODY, 1);
curl_setopt($ch, CURLOPT_FAILONERROR, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
if (curl_exec($ch) !== FALSE) {
return true;
} else {
return false;
}
}
推荐阅读
- regex - 在vim中查找和替换花括号之间的所有内容
- python-3.x - 尝试使用 kaggle API 时出现“-bash: command not found”
- c# - 如何从 ContentDisposition 获取扩展
- javascript - 将函数限制为仅使用“new”运算符调用
- node.js - 共享会话 Socket.io Express
- vba - 宏按钮在 Excel 中添加行(带有下面行中的公式),同时保持标题
- arrays - 在已知片段中拆分数组
- c++ - UWP:在 TreeNode 中将文本设置为粗体
- vba - 发现重复值时,VBA 计数并打印每个单元格地址
- awk - 如何增加文本文件中的时间戳