php - Force Downloading a PDF file, corrupt file -
i've got problem has risen many times on so, can't seem find solution mine! i'm trying deliver pdf file client without opening in browser, file downloads corrupt when open , missing quite few bytes original file. i've tried several such methods downloading file i'll show latest i've used , feedback.
i have opened downloaded pdf in text editor , there no php errors @ top of can see!
i'm aware readfile() quicker testing purposes desperate working used while(!feof()) approach!
anyway enough rambling, heres code (taken why downloaded file alwayes damaged or corrupted?):
$file = __dir__ . '/reports/somepdf.pdf'; $basename = basename($file); $length = sprintf("%u", filesize($file)); header('content-description: file transfer'); header('content-type: application/octet-stream'); header('content-disposition: attachment; filename="' . $basename . '"'); header('content-transfer-encoding: binary'); header('connection: keep-alive'); header('expires: 0'); header('cache-control: must-revalidate, post-check=0, pre-check=0'); header('pragma: public'); header('content-length: ' . $length); ob_clean(); set_time_limit(0); readfile($file);
also note difference in file size:
original: 351,873 bytes downloaded: 329,163 bytes
make sure you're not running compression output buffering handlers, such ob_gzhandler. had similar case , had disable output buffering work properly
Comments
Post a Comment