Ben Muircroft
Ben Muircroft

Reputation: 3034

pdf php download produces empty file

<?php
header('Content-disposition: attachment; filename=Booking.pdf');
header('Content-type: application/pdf');
readfile('http://mysite.com/Booking.pdf');
?>

why is the Booking.pdf file downloaded empty!??

mac and windows both say: The file “Booking.pdf” could not be opened because it is empty.

checked google and stackoverflow, can't find relative info... has anyone experienced this before?

ps: I only found this forum post:'The online issue is a bit off topic I think, but is generally due to loading the PDF to a server in the ASCII mode of FTP rather than binary. That creates a corrupt file. Be sure to turn on binary transmission', but this is not true in this case as i can display the same pdf file in an iframe and it is not blank/empty.

Upvotes: 1

Views: 3369

Answers (1)

Baba
Baba

Reputation: 95161

You need to change

  readfile('http://mysite.com/Booking.pdf');

To

  readfile(__DIR__ . '/Booking.pdf');

Example

$file = __DIR__ . '/test.pdf' ;
header('Content-disposition: attachment; filename=Booking.pdf');
header('Content-type: application/pdf');
header("Content-length: ".filesize($file));
readfile($file);

Upvotes: 4

Related Questions