file_get_contents() 和stream_get_contents() 无法打开流
我试图通过发送特殊标头来强制下载文件。为此,我必须通过下载脚本重定向 PDF 文档的 URL 请求。
我传递了一个名为 $seg3 的查询,该查询在发送之前进行了 base64_encode() 处理,然后在尝试请求文件时进行了 base64_decode() 处理和 urlencoded() 处理。
我已经查看过使用,
if (ini_get('allow_url_fopen') == '1')
{
$data = file_get_contents(urlencode(base64_decode($seg3)));
}
else
{
$fp = fopen(urlencode(base64_decode($seg3)), 'rb');
$data = stream_get_contents($fp);
fclose($fp);
}
但是 file_get_contents()
和 stream_get_content()
都失败了:
fopen($URL): failed to open stream: HTTP request failed! HTTP/1.1 404 Not Found
然而,当我转储正在发送的 URL 时,我可以复制并将其粘贴到我的浏览器中并打开该文件。
它似乎只在文件中存在空格时发生,但无论我是否使用 urlencode() ,都会发生错误。
I'm trying to force a file to be downloaded by sending it special headers. In doing so, I have to redirect URL requests for PDF documents through my download script.
I pass a query called $seg3, which is base64_encode()ed before sending, and then base64_decode()ed and urlencoded() when trying to request the file.
I've taken a look at using,
if (ini_get('allow_url_fopen') == '1')
{
$data = file_get_contents(urlencode(base64_decode($seg3)));
}
else
{
$fp = fopen(urlencode(base64_decode($seg3)), 'rb');
$data = stream_get_contents($fp);
fclose($fp);
}
But both file_get_contents()
and stream_get_content()
fail with:
fopen($URL): failed to open stream: HTTP request failed! HTTP/1.1 404 Not Found
Yet when I dump the URL that is being sent, I can copy and paste it in my browser and open the file.
It only seems to occur when spaces are in the file, yet the error occurs whether I use urlencode()
or not.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
url 可能会返回 404 错误,但仍会返回常规内容。因此,虽然浏览器可能会显示常规页面,但此功能将因结果代码而失败。
It may be that an url returns a 404 error, but still returns regular contents as well. So while the browser may display a regular page, this function will fail because of the result code.
我确信这个问题已经得到解决,但是如果有空格 urlencode($url) 可能会解决您的问题。
I'm sure this has been fixed already, but if there are spaces urlencode($url) might solve your problem.