correct PHP headers for pdf file download

PhpPdfHeader

Php Problem Overview


I'm really struggling to get my application to open a pdf when the user clicks on a link.

So far the anchor tag redirects to a page which sends headers that are:

$filename='./pdf/jobs/pdffile.pdf;

$url_download = BASE_URL . RELATIVE_PATH . $filename;

header("Content-type:application/pdf");
header("Content-Disposition:inline;filename='$filename");
readfile("downloaded.pdf");

this doesn't seem to work, has anybody successfully sorted this problem in the past?

Php Solutions


Solution 1 - Php

Example 2 on [w3schools][1] shows what you are trying to achieve.

> header("Content-type:application/pdf"); >
> // It will be called downloaded.pdf > header("Content-Disposition:attachment;filename='downloaded.pdf'"); >
> // The PDF source is in original.pdf > readfile("original.pdf"); > ?> >

Also remember that,

> It is important to notice that header() must be called before any > actual output is sent (In PHP 4 and later, you can use output > buffering to solve this problem)

[1]: http://www.w3schools.com/php/func_http_header.asp "w3schools"

Solution 2 - Php

$name = 'file.pdf';
//file_get_contents is standard function
$content = file_get_contents($name);
header('Content-Type: application/pdf');
header('Content-Length: '.strlen( $content ));
header('Content-disposition: inline; filename="' . $name . '"');
header('Cache-Control: public, must-revalidate, max-age=0');
header('Pragma: public');
header('Expires: Sat, 26 Jul 1997 05:00:00 GMT');
header('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT');
echo $content;

Solution 3 - Php

There are some things to be considered in your code.

First, write those headers correctly. You will never see any server sending Content-type:application/pdf, the header is Content-Type: application/pdf, spaced, with capitalized first letters etc.

The file name in Content-Disposition is the file name only, not the full path to it, and altrough I don't know if its mandatory or not, this name comes wrapped in " not '. Also, your last ' is missing.

Content-Disposition: inline implies the file should be displayed, not downloaded. Use attachment instead.

In addition, make the file extension in upper case to make it compatible with some mobile devices. (Update: Pretty sure only Blackberries had this problem, but the world moved on from those so this may be no longer a concern)

All that being said, your code should look more like this:

<?php

    $filename = './pdf/jobs/pdffile.pdf';

    $fileinfo = pathinfo($filename);
    $sendname = $fileinfo['filename'] . '.' . strtoupper($fileinfo['extension']);

    header('Content-Type: application/pdf');
    header("Content-Disposition: attachment; filename=\"$sendname\"");
    header('Content-Length: ' . filesize($filename));
    readfile($filename);

Technically Content-Length is optional but it is important if you want the user to be able to keep track of the download progress, and detect if the download was interrupted before the end. When using it you have to make sure you won't be send anything along with the file data. Make sure there is absolutely nothing before <?php or after ?>, not even an empty line.

Solution 4 - Php

I had the same problem recently and this helped me:

    header('Content-Description: File Transfer'); 
    header('Content-Type: application/octet-stream'); 
    header('Content-Disposition: attachment; filename="FILENAME"'); 
    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("PATH/TO/FILE")); 
    ob_clean(); 
    flush(); 
    readfile(PATH/TO/FILE);      
    exit();

I found this answer here

Solution 5 - Php

Can you try this, readfile need the full file path.

        $filename='/pdf/jobs/pdffile.pdf';            
		$url_download = BASE_URL . RELATIVE_PATH . $filename;            
		            
		//header("Content-type:application/pdf");	
        header("Content-type: application/octet-stream");			            
		header("Content-Disposition:inline;filename='".basename($filename)."'");			
		header('Content-Length: ' . filesize($filename));
        header("Cache-control: private"); //use this to open files directly			            
		readfile($filename);

Solution 6 - Php

You need to define the size of file...

header('Content-Length: ' . filesize($file));

And this line is wrong:

> header("Content-Disposition:inline;filename='$filename");

You messed up quotas.

Solution 7 - Php

header("Content-type:application/pdf");

// It will be called downloaded.pdf thats mean define file name would be show

header("Content-Disposition:attachment;filename=  $fileName  ");

// The PDF source is in original.pdf

readfile($file_url);

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionuseyourillusiontooView Question on Stackoverflow
Solution 1 - PhpgatView Answer on Stackoverflow
Solution 2 - PhpMarin VartanView Answer on Stackoverflow
Solution 3 - PhpHavenardView Answer on Stackoverflow
Solution 4 - PhphaayekView Answer on Stackoverflow
Solution 5 - PhpKrish RView Answer on Stackoverflow
Solution 6 - PhpFlash ThunderView Answer on Stackoverflow
Solution 7 - PhpAzad Education View Answer on Stackoverflow