PHP读取文件问题
我正在使用以下命令:
@header("Cache-Control: no-cache, must-revalidate");
@header("Content-Type: application/octet-stream");
@header("Expires: Sat, 26 Jul 1997 05:00:00 GMT");
@header("Content-Length: ".$row['file_size']);
@header("Content-Disposition: attachment; filename=\"".$row['artist'] . " - " .$row['title']."\"");
@header("Content-type: audio/mpeg;\r\n");
开始下载,现在由于某种原因它给了我
警告:readfile(http://theurl.com/downloads/the file somespaces.mp3) [function. readfile]:无法打开流:HTTP 请求失败! HTTP/1.1 404 未找到 在 /home/belgin/public_html/dl.php 的 29
行中
,现在网址有效,我已经三重检查,但这可能是因为中有空格文件名?如果是这样我该如何解决这个问题?感谢
重述行:
readfile(http://domain.com/downloads/Warp 1.9_The Bloody Beetroots_320000.mp3)
I'm using the following :
@header("Cache-Control: no-cache, must-revalidate");
@header("Content-Type: application/octet-stream");
@header("Expires: Sat, 26 Jul 1997 05:00:00 GMT");
@header("Content-Length: ".$row['file_size']);
@header("Content-Disposition: attachment; filename=\"".$row['artist'] . " - " .$row['title']."\"");
@header("Content-type: audio/mpeg;\r\n");
to start a download, now for some reason its giving me
Warning: readfile(http://theurl.com/downloads/the file some spaces.mp3) [function.readfile]: failed to open stream: HTTP request failed! HTTP/1.1 404 Not Found
in /home/belgin/public_html/dl.php on line 29
now the url is valid, I've triple checked but could it be because there's spaces in the file name? And if so how can i fix that? Thanks
reafile line :
readfile(http://domain.com/downloads/Warp 1.9_The Bloody Beetroots_320000.mp3)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
貌似url里有空格。您是否正确转义它以便在 readfile() 中使用?
It seems that the url contains spaces. Do you escape it properly for use inside readfile()?
不要对网址进行编码!它将像
http%3A%2F%2Fexample.com%2Fsome%20song.mp3
进行编码。您只需要替换空格即可。所以请使用str_replace
代替...readfile(str_replace(" ", "%20", $url));
DO NOT encode url! It will encode like
http%3A%2F%2Fexample.com%2Fsome%20song.mp3
. You just need to replace spaces. so usestr_replace
instead...readfile(str_replace(" ", "%20", $url));
urlencode() 不是正确的方法。如上所述,只需使用 str_replace() 即可。
这是为什么它不起作用的另一种可能性。这就是我的原因。有趣的是, file_exists 返回 true,但如果没有正确设置以下内容,则任何形式的向公众提供文件下载的服务都无法正常工作。
PHP 有一个名为 open_basedir 的设置,
请确保该设置与您的托管环境相关正确。 open_basedir 可以通过 php.ini 进行编辑
urlencode() is not the way to go. As mentioned above, simply use str_replace().
Just another possibility here as to why its not working. This was the cause for me. Interestingly, file_exists was returning true but no form of serving the file to the public for download was working without having the below set correctly.
PHP has a setting called open_basedir
Make sure this is set correctly relevant to your hosting environment. open_basedir can be edited via php.ini
我认为你需要 urlencode() 和 html_entities() url 或文件名或其他
I think your going to need to urlencode() and html_entities() the url or filename or whatever