2

im trying to make pdf files downloadable in my website but all im getting is the current page source(html). the file name is correctly given but the file itself is not downloading.

ive tried various fixes found on stackoverflow but its not helping.

ive tried AddType application/octet-stream .pdf in htaccess , also ForceType. Tried the php fix here: How to make PDF file downloadable in HTML link? and going through php with this:

header("Content-disposition: attachment; filename=filename.pdf");
header("Content-type: application/pdf");
readfile("filename.pdf");

and then linking to the php file, still the same.

what am i doing wrong and what information do you require to make better sense of this?

Community
  • 1
  • 1
xerr0n
  • 23
  • 2

3 Answers3

0

You can have serveral mistakes to check (and debug) try this

  <?php
     $file = ABSOLUTE_PATH_WHERE_PDF_IS_STORED.'/my.pdf'; //replace *ABSOLUTE_PATH_WHERE_PDF_IS_STORED* with your path

     if (file_exists($file)) {
         header('Content-Description: File Transfer');
         header('Content-Type: application/octet-stream');
         header('Content-Disposition: attachment; filename='.basename($file));
         header('Expires: 0');
         header('Cache-Control: must-revalidate');
         header('Pragma: public');
         header('Content-Length: ' . filesize($file));
         readfile($file);
         exit;
  } else {
      die("FILE [".$file."]" don't exists!");
  }
 ?>
donald123
  • 5,638
  • 3
  • 26
  • 23
  • using this it gives the die response, ie file does not exist. – xerr0n Jul 17 '15 at 20:28
  • Correction: using this code correctly, didn’t add the absolute path correctly the first time, makes the browser download the file, which is an html file (the front-page source to be exact) – xerr0n Jul 17 '15 at 20:41
0

So i found the problem, which is of course quite obvious, i linked to the files wrongly, and since im using a cms it sent the front page source (default behaviour). Peculiarly the html5 download attribute doesn’t work anyway.

Thank you Donald123 and PKa for answering.

xerr0n
  • 23
  • 2
-1

You can use a tag with attribute download

<a href="path/to/file/*.pdf" download>Download this pdf</a>

Its a Quick way to do that.

PKa
  • 309
  • 2
  • 7
  • 23
  • 1) download is a *html5* attribute and not supportet in all browser! 2) this solves not the question of the OP – donald123 Jul 17 '15 at 13:18
  • as he said in first line, he wants to make pdf files downloadable. I just show him an easy way to do that. – PKa Jul 17 '15 at 13:21
  • using the HTML5 or the older version of just linking to it gives the same output. – xerr0n Jul 17 '15 at 20:25