Jump to content

[SOLVED] Help: problem with Headers to download PDF file


Recommended Posts

Hi,

 

I'm trying to implement a script where when a user clicks a link he gets a PDF file to open or save.

 

In my index.php page I have the following link:

 

<a href="http://localhost/example/get_file.php">Click here to get the file</a>

 

And the script is in get_file.php:

 

<?php
$path = '/docs/document.pdf';
$mm_type="application/pdf"; 


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: " . filesize($path) );
header('Content-Disposition: attachment; filename="'.basename($path).'"');
header("Content-Transfer-Encoding: binary");

readfile($path); 

exit();
?>

 

Unfortunately it's not working properly. When clicking on the link and either selecting 'open' or 'save' from the dialog box, the end result is always the same; the end file is about 1KB in size when originally it was 456KB and therefore it's damaged.

 

Any ideas on why this is happening?

 

Thanks!

 

This works for me for downloading all file types:

 

$path = "/folder/file.pdf";

$file = basename($path);

 

if(file_exists($path)) {

header("Cache-Control: public");

header("Content-Description: File Transfer");

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

header("Content-Transfer-Encoding: binary");

readfile($path);

}

Mates, thanks a lot for your help!!!

 

I have found the problem thanks to you johnsmith153.

 

I have tried your code and I realized that the path was wrong!

 

The "file_exists($path)" code warned me of the problem.

 

I had to include in the path the "http://localhost" reference in order to read the file.

 

 

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.