PHP读取文件问题

发布于 2024-09-25 14:54:44 字数 785 浏览 2 评论 0原文

我正在使用以下命令:

@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.php29
行中

,现在网址有效,我已经三重检查,但这可能是因为中有空格文件名?如果是这样我该如何解决这个问题?感谢

重述行:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

凌乱心跳 2024-10-02 14:54:45

貌似url里有空格。您是否正确转义它以便在 readfile() 中使用?

It seems that the url contains spaces. Do you escape it properly for use inside readfile()?

策马西风 2024-10-02 14:54:45

不要对网址进行编码!它将像 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 use str_replace instead...

readfile(str_replace(" ", "%20", $url));

迷你仙 2024-10-02 14:54:45

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

﹎☆浅夏丿初晴 2024-10-02 14:54:44

我认为你需要 urlencode() 和 html_entities() url 或文件名或其他

I think your going to need to urlencode() and html_entities() the url or filename or whatever

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文