PHP:Readfile 在本地主机上工作,但在服务器上不起作用
我花了几个小时试图解决我的问题并搜索了谷歌和几个主板,但我还没有找到解决方案。
我的问题:我构建了一个生成下载的 PHP 脚本。 代码如下:
$file = "file.pdf";
$download_folder = "../contents/"; //RELATIV
$type= mime_content_type($download_folder.$file);
header("Content-Type: $type");
header("Content-Disposition: attachment; filename=\"$file\"");
readfile($download_folder.$file);
如果我在本地主机服务器(xampp)上尝试它,它就会工作并且开始下载文件。如果我将脚本上传到我的托管服务器(不是自己的,它已经消失了),我只会得到一个空白页面。
有什么想法吗?谢谢!
I tried to solve my problems for hours and searched google and several boards, but i haven't found a solution.
My Problem: I built a PHP-script what generates a download.
Code below:
$file = "file.pdf";
$download_folder = "../contents/"; //RELATIV
$type= mime_content_type($download_folder.$file);
header("Content-Type: $type");
header("Content-Disposition: attachment; filename=\"$file\"");
readfile($download_folder.$file);
If I try it at my localhost server (xampp) it works and the download of the file begins. If I upload the script to my hosting server (not an own, it's goneo) i only get a blank page.
Any ideas? Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
检查权限。
如果您有该文件的读取权限。
您可以使用 chmod 命令来设置权限。
Check for the permission.
If you have read permission on the file.
you can use chmod command to set permission.
确保您已打开错误报告并在开发过程中显示所有内容:
这应该告诉您问题是什么。
我的猜测是路径有问题。
尝试使用绝对路径,例如
/var/www/vhosts/yoursite.com/httpdocs/contents/file.pdf
。如果失败,则可能是权限问题。
Make sure you've got error reporting turned on and are displaying everything during development:
That should tell you what the problem is.
My guess is that it's having an issue with the path.
Try using an absolute path such as
/var/www/vhosts/yoursite.com/httpdocs/contents/file.pdf
.If that fails, then its probably a permissions issue.
尝试
在执行
header()
调用之前添加。您很可能已将文件上传到该脚本期望的位置之外的位置,或者网络服务器无法读取该文件。try adding
before doing the
header()
calls. Most likely you've uploaded the filesomewhere other than where this script expects it to be, or isn't readable by the webserver.发现错误了。在网络服务器上没有函数 mime_content_type 。感谢您的帮助
Found the mistake. At the webserver wasn't the function mime_content_type .Thanks for your help