0

I try to offer a pdf-file-download, handeled in dwhandler.php. All this is supposed to do is open the pdf-file or start a download of it. I tried:

<?php
header("Content-Description: File Transfer");
header("Content-Type: application/octet-stream");
header("Content-Disposition: attachment; filename='Paper.pdf'");
header("Cache-Control: must-revalidate");
readfile("Paper.pdf");
exit;
?>    

I also tried some other headers, but none worked.

The browser keeps opening the file directly in some weird coding. Here is a pic of the first few lines: http://www.directupload.net/file/d/3865/h8waniq9_jpg.htm

Has anybody an idea why this is happening? I have no idea what is left to try...

Thank you very much!

Zorakh
  • 1
  • 3

2 Answers2

1

is html not a option? I know you can offer files to download with simply using html 5 by doing

<a href="example.pdf" download>download not open it</a>

Take a look into: Forcing to download a file using PHP

Community
  • 1
  • 1
  • Problem is that I need to send an SQL query right before starting the download. So I would prefer having an extra file, I can't think of any html solution being really satisfying. And I guess it should be possible to fix this :/ – Zorakh Jan 12 '15 at 10:37
  • Additionaly the file is only available of logged in :/ I looked through your link, though I already tried this and it didn't work out – Zorakh Jan 12 '15 at 10:48
  • is the file only available when logged in, or the page? As it is the page, you can fix that with sessions. as it is for the file only, you indeed have to use php –  Jan 12 '15 at 10:49
  • By now, none of both, i kicked everything beside the download routine in order to check it. Though the aim is that PHP file is session protected and the PDF file is not externly accessable -> protected by .htaccess – Zorakh Jan 12 '15 at 11:09
  • have you checked the link I send you? –  Jan 12 '15 at 11:09
  • Jep, didn't solve the problem, though I will try to create another directory with .htacess forcing the file type. I don't really think it is going to help though. Is it possible that my server does not allow file download from certain directorys? – Zorakh Jan 12 '15 at 11:14
  • Since code like this seema to ne working for others I can only assume the Server disturbes it.. Sadly it's a 1and1 web space so I can't config too much. – Zorakh Jan 12 '15 at 11:17
  • Ty!Is there a possibility to include a htaccess protected PDF in another file on the server... So I could session protect this file and the PDF would only be available via it... – Zorakh Jan 12 '15 at 11:22
0
<?php 

    // Do your Sql query
    header('Location: http://host/files/pdf/file.pdf');   // redirect user to actualy location

?>
ElGavilan
  • 6,610
  • 16
  • 27
  • 36
Tiger
  • 404
  • 1
  • 4
  • 13