PHP file download including the .php file when only pdf is wanted


$file = $_GET['name'];

$path = './curr/'.$file.'.pdf'; // the file made available for download via this PHP file
$mm_type="application/pdf"; // modify accordingly to the file type of $path, but in most cases no need to do so

header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: public");
header("Content-Description: File Transfer");
header("Content-Type: " . $mm_type);
header("Content-Length: " .(string)(filesize($path)) );
header('Content-Disposition: attachment; filename="'.basename($path).'"');
header("Content-Transfer-Encoding: binary\n");

readfile($path); // outputs the content of the file


This is a snippet of code in file.php. I am referring to the file using:

<a href="file.php?name=First File">File 1</a>

The intent is that on click of the link,./curr/First File.pdf should download. I do get a download, but on inspecting, it's the webpage with the pdf embedded in the file. Could anyone assist?




If you want to have just the PDF loaded, the above code is all code to be executed.

Drop all surrounding menus, header or footers. Make sure, that no HTML or any other output besides the PDF fromreadfile() remains, when calling this link.




Try to change the content type to :

header("Content-Type: application/force-download");

People are also looking for solutions to the problem: math - PHP reordering array


Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.