rad mad
rad mad

Reputation: 11

I am having trouble with my php download code

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?

Upvotes: 0

Views: 616

Answers (3)

Flygenring
Flygenring

Reputation: 3848

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');

Upvotes: 0

Phil
Phil

Reputation: 164870

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

This, get rid of this.

Upvotes: 1

Nikson Kanti Paul
Nikson Kanti Paul

Reputation: 3440

Do not echo before header content sent.

just remove it

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

Upvotes: 1

Related Questions