0

I am making a website where users can upload, read and download pdf files. The upload is working well, they can read the pdf file online and can also download it using the adobe reader plugin. But the download code I wrote is giving me a hard time. It downloads the file but the file doesn't open in the adobe reader. It gives an error "Adobe Reader could not open file because it is either not a supported file type or because the file has been damaged".

Here is my code:

if(is_file($fullPath)) {
  $fsize = filesize($fullPath);
  $path_parts = pathinfo($fullPath);
  $ext = strtolower($path_parts['extension']);
  switch($ext) {
    case 'pdf':
      header('Content-type: application/pdf');
      header('Content-Disposition: attachment; filename="' . $fullPath. '"');
      break;
    default:
      header('Content-type: application/octet-stream');
  }
  header('Content-length: $fsize');
  header('Cache-control: private'); //use this to open files directly
  readfile($name);
}
exit;

could some one help me out with it?

Bo Persson
  • 90,663
  • 31
  • 146
  • 203
rad mad
  • 11
  • 1
  • could some one please help me out with this. I have bin trying to solve this problem since couple of days. – rad mad Jul 16 '12 at 11:17
  • If you give an example of the content of `$name` and `$fullPath` it might be easier to help. As it looks, I'd say that you should use `$name` in the Content-Disposition header and `$fullPath` in `readfile()` – Flygenring Jul 16 '12 at 17:15

3 Answers3

1

echo "<br>$fullPath<br>";

This, get rid of this.

Phil
  • 157,677
  • 23
  • 242
  • 245
1

Do not echo before header content sent.

just remove it

echo "<br>$fullPath<br>";
Nikson Kanti Paul
  • 3,394
  • 1
  • 35
  • 51
0

As the others said, headers are sent at the first output if nothing else is specified, so your echo will trigger the script to send the standard headers for the content type, as specified by the web server - probably html or something like it.

Furthermore you have other unnecessary stuff in you code, so I'd suggest something like this:

if(is_file($fullPath)) {
  $path_parts = pathinfo($fullPath);
  $ext = strtolower($path_parts['extension']);
  switch($ext) {
    case 'pdf':
      header('Content-type: application/pdf');
      header('Content-Disposition: attachment; filename="' . $name . '"');
      break;
    default:
      header('Content-type: application/octet-stream');
      header('Content-length: ' . filesize($fullPath));
      header('Cache-control: private'); //use this to open files directly
  }
  readfile($fullPath);
}
exit;

The biggest differences is, that the initial check just checks if the file exists and doesn't open the file. The output is done by just opening the file and writing it directly to the output buffer. I removed the partial Content-Disposition header from the default case, as it will by default do the same or better, and moved Content-lenght and Cache-control to the default case, as they're not needed for the pdf download. Also, I changed it to serve the file with filename $name. I suspect it should be the other way around if $name is defined outside the given scope.

If the script fails, you may just have the wrong path to the pdf-file. Try putting this code before the one above

echo 'The file "' . $fullPath . '" ' . (is_file($fullPath) ? 'exists' : 'doesn\'t exist');
Flygenring
  • 3,818
  • 1
  • 32
  • 39
  • thanks but i still get the same error. the $name is taken from the database and gives the name of the file while download, the $fullpath is the path of the file so i dont want it to be the name for the file while downloading. – rad mad Jul 16 '12 at 08:10
  • the error also tells this "the file might not have been correctly decoded". – rad mad Jul 16 '12 at 08:11
  • I just updated my answer to use the method shown in [the first example in the `PHP Header Documentation`](http://php.net/manual/en/function.header.php#example-4117) – Flygenring Jul 16 '12 at 09:02
  • could you please help me out with this. I have bin trying to solve this problem since couple of days. – rad mad Jul 16 '12 at 11:17
  • I think the solution depends on what other code you've got in you script. If you output anything else or have the wrong file path it would break it. I'd like to help you, but with the information you have given so far (and according to my own tests), it should work. – Flygenring Jul 16 '12 at 12:03
  • ok heres a simple code but this is also not working i get the same error – rad mad Jul 17 '12 at 13:47
  • please help me out. I put up a code with a simple path as an answer even that gives me the same result.... the error i get is.... "Adobe Reader could not open 'note.pdf' because it is either not a supported file type or because the file has been damaged (for exmaple, it was sent as an email attachment and wasn,t correctly decoded)." – rad mad Jul 19 '12 at 06:15