Piotr Chabros Piotr Chabros - 1 year ago 79
PHP Question

PHP readfile() returning corrupted data

In my function I am saving an image decoded from a base64 string:

function saveImage(){
//magic...
define('UPLOAD_DIR', '../webroot/img/');
$base64string = str_replace('data:image/png;base64,', '', $base64string);
$base64string = str_replace(' ', '+', $base64string);
$data = base64_decode($base64string);
$id = uniqid();
$file = UPLOAD_DIR.$id.'.png';
$success = file_put_contents($file, $data);
}


The above function works properly and the images are saved and not corrupted in the specified folder.

In the next function I am now trying to force download the image to a user:

function getChart($uniqid= null){
if($uniqid){
$this->layout = null;
header("Content-type: image/png");
header("Content-Disposition:attachment;filename='".$uniqid.".png'");
readfile('../webroot/img/'.$uniqid.'.png');
exit;
} else exit;
}


Image downloaded from the server is corrupted and cant be displayed. After opening the downloaded file in a text editor I noticed that a new line character is added at the very top. After deleting the character and saving the file it opens properly and is being displayed properly.

How can I fix this?

Answer Source

What you describe can have multiple issues that are hidden until you actually open the downloaded file.

Instead make your code more robust and check pre-conditions, here if headers have been send already and to clean any possible existing output buffer and give error if that is not possible:

function getChart ($uniqid = null) {

    if (!$uniqid) exit;

    $this->layout = null;

    if (headers_sent()) throw new Exception('Headers sent.');
    while (ob_get_level() && ob_end_clean());
    if (ob_get_level()) throw new Exception('Buffering is still active.');

    header("Content-type: image/png");
    header("Content-Disposition:attachment;filename='".$uniqid.".png'");
    readfile('../webroot/img/'.$uniqid.'.png');

    exit;
}
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download