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 s开发者_StackOverflow社区olution.
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!
Check for the permission. If you have read permission on the file. you can use chmod command to set permission.
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
.
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. 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
精彩评论