Glenn
Glenn

Reputation: 551

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

<?php

$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?

Upvotes: 0

Views: 265

Answers (2)

Sirko
Sirko

Reputation: 74046

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 from readfile() remains, when calling this link.

Upvotes: 1

Mohamed Amine
Mohamed Amine

Reputation: 2304

Try to change the content type to :

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

Upvotes: 0

Related Questions