Reputation: 215
I have a lightbox with a form, when the user sends the form a download should start, this is the code I use:
function start_download( $path, $item ) {
$file = $path.$item;
header("Content-type:application/pdf");
header('"Content-Disposition:attachment;filename="'.$file.'"');
}
Unless the fact that is a function is a problem, I think it should work right? well it doesn't. No error whatsoever.
Looking at Chrome's developer tools I can see that the headers are actually set application/pdf
.
Oh, also, when I add readfile($file)
it seems to read the file but it returns a strange string (numbers and weird symbols).
I searched over this site but nothing seems to work. I really don't know what else can I do. Ideas?
BTW if I "echo" the $file
it shows the url correctly, I don't think that is the problem.
Upvotes: 0
Views: 1287
Reputation: 1537
Try the following:
function start_download( $path, $item ) {
$file = $path.$item;
if (file_exists($file)) {
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename=' . $item);
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
header('Content-Length: ' . filesize($file));
ob_clean();
flush();
if (readfile($file) !== FALSE) return TRUE;
} else {
die('File does not exist');
}
}
Upvotes: 0
Reputation: 360672
You've got wonky quotes, for one
header('"Content-Disposition:attachment;filename="'.$file.'"');
^^--- why double quoting?
They're breaking the header call.
Try:
header("Content-Disposition: attachment; filename=$file");
Note that I've put some spaces in there. They're strictly speaking not necessary, but they do help with legibility.
Upvotes: 1
Reputation: 131881
function start_download( $path, $item ) {
$file = $path.$item;
header("Content-Type: application/pdf");
header('Content-Disposition: attachment;filename="'.basename($file) . '"');
readfile($file);
}
As far as I can see this may work, as long as $file
is a valid local path name to a pdf file. Make sure, there is absolutely no other output!
Upvotes: 0