简体   繁体   中英

PDF Downloading not functioning properly

I am using the following code to download a PDF....

   header('Content-Type: application/pdf');
   header('Content-Disposition: attachment; filename="NewName.pdf"');
   readfile($root_path.'/full-tile-book.pdf');

It's a 13 MB file and only downloads like 130k and of course can't be opened. How do I debug what the issue is here? My path to the pdf is correct.

Perhaps you are running into a memory/filesize error? I've had problems in the past dumping large files with readfile. In addition to setting the Content-Length header, I recommend using fpassthru() , as it does not read the file into a buffer, it just dumps it.

set_time_limit(0); // disable timeout
$file = $root_path.'/full-tile-book.pdf';
header('Content-Type: application/pdf');
header('Content-Disposition: attachment; filename="NewName.pdf"');
header('Content-Length: ' . filesize($file));
session_write_close(); // remove this line if sessions are not active
$f = fopen($file, 'rb');
fpassthru($f);
fclose($f);
exit;

EDIT: If you are using any sessions_code, it is a good idea to end the session before starting the file dump process. I have updated my example to reflect this.

I'd add a few items. Assign the filename to $file, then try:

header('Content-Length: ' . filesize($file));
ob_clean();
flush();
readfile($file);

This will ensure that the client is expecting the correct length, and the output buffer flushes nicely in between.

see downloaded file perhaps have bad address use in PHP.
You can follow this tutorial for file download in PHP.

HTML headers must be sent before any output is sent to the browser. PHP uses the header function to pass raw HTML headers. For this example we're going to get the filename from the URL www.yourdomain.com/download.php?file=download.zip.

<? 
$dir="/path/to/file/";
if (isset($_REQUEST["file"])) {
    $file=$dir.$_REQUEST["file"];
    header("Content-type: application/force-download");
    header("Content-Transfer-Encoding: Binary");
    header("Content-length: ".filesize($file));
    header("Content-disposition: attachment; filename="".basename($file).""");
    readfile("$file");
} else {
    echo "No file selected";
}
 ?>

We started with setting the directory where the files to be downloaded are located in $dir. Be sure not to use in $dir. Then we checked to make sure a filename was specified in the request. If a file was specified then we set $file to the path to the file and the filename. Now that the prep work is done its time to send the file to the browser.

The first header statement tells the browser to expect a download. The next two header statements tell the browser the format of the data and the size of the file respectively. The last header statement tells the browser the name of the file. Finally the readfile statement sends the file to the browser.

More information.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM