I have a function that returns an image from a database, the image is 100% fine in the database however when it is sent to the browser a newline (return) character is inserted in the very beginning and it corrupts the file.
The code to return the file is:
//if(ob_get_length() > 0)
//{
//ob_clean();
//}
Commented the above out as when it is uncommented it doesn't even let the browser download the file.
function header_file($data, $file_data)
{
$last_modified = gmdate('D, d M Y H:i:s', $file_data['unix_last_modified_time'])." GMT";
// update the accessed record
$GLOBALS['db']->query("update file_uploads set file_upload_accessed_count = file_upload_accessed_count + 1 where file_upload_id = '".$GLOBALS['id']."'");
// if browser question if it's up to date
if (isset($_SERVER['HTTP_IF_MODIFIED_SINCE']))
{
// parse header
$if_modified_since = preg_replace('/;.*$/', '', $_SERVER['HTTP_IF_MODIFIED_SINCE']);
if ($if_modified_since == $last_modified)
{
// the browser's cache is still up to date
header("HTTP/1.0 304 Not Modified");
header("Cache-Control: max-age=86400, must-revalidate");
exit;
}
}
header("Cache-Control: max-age=86400, must-revalidate");
header("Last-Modified: ".$last_modified);
header("Content-Type: ".$file_data['file_upload_type']);
if($file_data['file_upload_type'] == 'application/x-shockwave-flash')
header("Content-Disposition: inline; filename=\"".str_replace(' ','_',$file_data['file_upload_name'])."\"");
else
header("Content-Disposition: attachment; filename=\"".str_replace(' ','_',$file_data['file_upload_name'])."\"");
// send data to output
echo $data;
exit;
}
Related Questions: Corrupted File from PHP Header File returned from database? https://stackoverflow.com/questions/19768650/zend-caching-of-images-gives-problems-once-the-site-goes-down-for-a-while