Php Can';t将远程文件下载到服务器
远程文件是附件,我可以获取标题,但无法获取文件或其内容Php Can';t将远程文件下载到服务器,php,attachment,Php,Attachment,远程文件是附件,我可以获取标题,但无法获取文件或其内容 <?php error_reporting(E_ALL); //not working, with rb too //file_put_contents("natives.html", stream_get_contents(fopen("http://www.dev-c.com/nativedb/reference.html", 'r'))); //same with rb too //
<?php
error_reporting(E_ALL);
//not working, with rb too
//file_put_contents("natives.html", stream_get_contents(fopen("http://www.dev-c.com/nativedb/reference.html", 'r')));
//same with rb too
//file_put_contents("natives.html", fopen("http://www.dev-c.com/nativedb/reference.html", 'r'));
//same
//echo file_get_contents("http://www.dev-c.com/nativedb/reference.html");
//same
/*$url = 'http://www.dev-c.com/nativedb/reference.html';
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_BINARYTRANSFER, false);
curl_setopt($curl, CURLOPT_HEADER, true);
$data = curl_exec($curl);
curl_close($curl);
echo $data;*/
//same
$url = 'http://www.dev-c.com/nativedb/reference.html';
$file = basename($url);
$fp = fopen($file, 'w');
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_FILE, $fp);
$data = curl_exec($ch);
curl_close($ch);
fclose($fp);
?>
因此,如果我使用这些代码中的任何一个,输出都是空的,并且在我的Web服务器上创建的文件是空的。
文件:我提取了您随示例提供的web URL的标题。这就是我得到的:
HTTP/1.1200ok
服务器:nginx/1.12.0
日期:2017年6月23日星期五11:34:58 GMT
内容类型:应用程序/八位字节流
**内容长度:0**
连接:保持活力
内容描述:reference.html
内容处置:附件;filename=reference.html
内容传输编码:二进制
到期时间:2017年6月22日星期四11:34:58 GMT
缓存控制:必须重新验证
布拉格语:公共
设置Cookie:PHPSESSID=f9d4eb515eeabfb461a11d056fdc7b78;路径=/
缓存控制:最大年龄=3600,专用,代理重新验证
如您所见,内容长度:0是零。这可能是你得到一个空身体的原因之一
如果您有权访问托管reference.html的NGINX服务器,则可以在尝试下载reference.html时尝试转储/var/log/NGINX/error.log的内容
如果内容长度为零,则没有可下载的文件大小。它指出服务器设置有问题。谢谢您的帮助,但问题在于编码。 我必须发送标题“Accept Encoding:gzip”,然后对输出进行gzip解码
<?php
error_reporting(E_ALL);
$url = 'http://www.dev-c.com/nativedb/reference.html';
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_BINARYTRANSFER, true);
curl_setopt($curl, CURLOPT_HEADER, false);
curl_setopt($curl, CURLOPT_HTTPHEADER, array(
"Host: www.dev-c.com",
"Accept-Encoding: gzip"
));
$data = curl_exec($curl);
curl_close($curl);
echo gzdecode($data);
?>
问题出在哪里?文件是空的吗?@RaviGehlot没有它就不能工作http://Is 文件为空?@RaviGehlot本地文件始终为空您可以打印phpinfo()的内容并告诉我您的open_basedir值是多少?为什么我可以通过浏览器下载?我认为nginx没有问题。如果您可以访问服务器,您可以放置一个小文件(比如test.txt)。类似于一个文本文件,只有一个像“Hello World”这样的小字符串。可以使用PHP脚本下载吗?有时候,NGINX会在更大的文件上出错。明白了。我想让你在服务器上禁用gzip。但这是个好主意。