Reputation: 148
I am using the following codes in my controller to download pdf files which are stored in a folder inside application folder in codeigniter. In the view page I have a button, upon clicking it, it redirects to this controller and then it download the file. It downloads blank page(pdf). Looking forward to any suggestion.
$filePath = APPPATH.'invoice/'."Let's Sunday#".$this->uri->segment(4).".pdf";
if(!empty($filePath) && file_exists($filePath)){
header("Content-Type: application/octet-stream");
header("Content-Disposition: attachment; filename=" .$filePath);
header("Content-Type: application/octet-stream");
header("Content-Type: application/download");
header("Content-Description: File Transfer");
header("Content-Length: " . filesize($filePath));
flush();
$fp = fopen($filePath, "r");
while (!feof($fp))
{
echo fread($fp, 65536);
flush();
}
fclose($fp); }else{echo "file does not exist";}
Upvotes: 0
Views: 364
Reputation: 3799
Here is what I have in a download section within a CodeIgniter app:
if (file_exists($filePath)) {
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename="'.basename($filePath).'"');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($filePath));
readfile($filePath);
flush();
exit;
}
The differences are in the ordering of the headers (which likely doesn't matter), the inclusion of the expires and pragma headers, and the Content-Disposition
using the full basename.
The naming convention on that PDF filename might cause an issue...something else to test out.
Upvotes: 0
Reputation: 4719
Change the name in Content-Disposition
to filename only instead of full path
Upvotes: 1