简体   繁体   English

PHP:Readfile在本地主机上有效,但在服务器上不可用

[英]PHP: Readfile works at localhost but not at server

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. 我的问题:我构建了一个PHP脚本来生成下载。 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. 如果我在我的本地服务器(xampp)上尝试了它,它将工作,并且开始下载文件。 If I upload the script to my hosting server (not an own, it's goneo) i only get a blank page. 如果我将脚本上载到托管服务器(而不是自己的服务器),则只会得到一个空白页。

Any ideas? 有任何想法吗? Thanks! 谢谢!

Check for the permission. 检查权限。 If you have read permission on the file. 如果您对该文件具有读取权限。 you can use chmod command to set permission. 您可以使用chmod命令设置权限。

Make sure you've got error reporting turned on and are displaying everything during development: 确保您已打开错误报告并在开发过程中显示所有内容:

ini_set('display_errors', 1);
error_reporting(E_ALL);

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 . 尝试使用绝对路径,例如/var/www/vhosts/yoursite.com/httpdocs/contents/file.pdf

If that fails, then its probably a permissions issue. 如果失败,则可能是权限问题。

try adding 尝试添加

if (!file_exists($download_folder . $file)) {
   die("can't find the file")
}
if (!is_readable($download_folder . $file)) {
   die("can't read the file")
}

before doing the header() calls. 在进行header()调用之前。 Most likely you've uploaded the filesomewhere other than where this script expects it to be, or isn't readable by the webserver. 您很可能已将文件上传到了该脚本期望的位置以外的其他位置,或者该文件无法被网络服务器读取。

Found the mistake. 发现了错误。 At the webserver wasn't the function mime_content_type .Thanks for your help 在网络服务器上不是mime_content_type函数。感谢您的帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM