PHP Readfile() Causing Corrupt File Downloads

PHP readfile() causing corrupt file downloads

First of all, as some people pointed out on the comments, remove all spaces before the opening PHP tag (<?php) on the first line and that should do the trick (unless this file is included or required by some other file).

When you print anything on the screen, even a single space, your server will send the headers along with the content to be printed (in the case, your blank spaces). To prevent this from happening, you can:

a) not print anything before you're done writing the headers;

b) run an ob_start() as the first thing in your script, write stuff, edit your headers and then ob_flush() and ob_clean() whenever you want your content to be sent to the user's browser.

In b), even if you successfully write your headers without getting an error, the spaces will corrupt your binary file. You should only be writing your binary content, not a few spaces with the binary content.

The ob_ prefix stands for Output Buffer. When calling ob_start(), you tell your application that everything you output (echo, printf, etc) should be held in memory until you explicitly tell it to 'go' (ob_flush()) to the client. That way, you hold the output along with the headers, and when you are done writing them, they will be sent just fine along with the content.

PHP readfile() corrupts file

Figured it out. I simply added ob_get_clean(); before readfile(); and ob_end_flush(); after it.

PHP Force downloading produces corrupt files

Your code is good. But what you are downloading is a fatal error, not the image:

<br />
<b>Fatal error</b>: Call to undefined function fileread() in <b>/var/www/html/test.php</b> on line <b>18</b><br />

Change fileread($file); with readfile($file);, and it should work.

Next time you have a "corrupt file of 140 bytes", try to open it as a text file.

Force file download with header is corrupting file

Like Barmar said, I needed to exit();/die(); after the function readfile();.

So the fix would look like:

header("Cache-Control: public");
header("Content-Description: File Transfer");
header("Content-Disposition: attachment; filename={$qfile['rfname']}");
header("Content-Type: application/zip");
header("Content-Transfer-Encoding: binary");
readfile($file['dl_path']);
exit(); // Or you can use die(); here

Force Downloading a PDF file, corrupt file

Make sure you're not running any compression output buffering handlers, such as ob_gzhandler. I had a similar case and I had to disable output buffering for this to work properly

Download of .zip file runs a corrupted file php

This issue can have several causes. Maybe your file is not found or it can not be read and thus the file’s content is just the PHP error message. Or the HTTP header is already sent. Or you have some additional output that then corrupts your file’s content.

Try to add some error handling into your script like this:

$file='../downloads/'.$filename;
if (headers_sent()) {
echo 'HTTP header already sent';
} else {
if (!is_file($file)) {
header($_SERVER['SERVER_PROTOCOL'].' 404 Not Found');
echo 'File not found';
} else if (!is_readable($file)) {
header($_SERVER['SERVER_PROTOCOL'].' 403 Forbidden');
echo 'File not readable';
} else {
header($_SERVER['SERVER_PROTOCOL'].' 200 OK');
header("Content-Type: application/zip");
header("Content-Transfer-Encoding: Binary");
header("Content-Length: ".filesize($file));
header("Content-Disposition: attachment; filename=\"".basename($file)."\"");
readfile($file);
exit;
}
}


Related Topics



Leave a reply



Submit