I have issue with printing session saved image data back in Drupal CMS. The result page get 6 bytes content not my uploaded image data.
The image storing code is :
// Read content of the uploaded file
$file_content = file_get_contents($_FILES["image_field"]["tmp_name"]);
// Strore the file in the session
$_SESSION['session_image'] = $file_content;
Then in another page, I'm printing the session stored data. The code :
// Set content type - octet-stream
header("Content-type: application/octet-stream");
// Print the session stored image back
echo $_SESSION['session_image'];
// Exit
exit;
I don't want to do the followings as a solution:
What I want is to print the uploaded file content as it is to the browser (as a octet stream). I really appreciate if any one can help on this.
Updated Code :
// Read content of the uploaded file
$file_content = file_get_contents($_FILES["image_field"]["tmp_name"]);
// Strore the file in the session
$_SESSION['session_image'] = base64_encode($file_content);
// Modified to have base64 encoded content to store so decode it here
$content = base64_decode($_SESSION['session_image']);
header("Content-Transfer-Encoding: binary");
header("Content-Length: ". mb_strlen($content, 'latin1'));
// Set content type - octet-stream
header("Content-type: application/octet-stream");
// Print content
echo $content;
exit;
But still the image content is not visible. Some of the characters of the image content are converted into other when uploaded image content and the original image content compared.
Try adding those:
header("Content-Transfer-Encoding: binary");
header("Content-Length: ". mb_strlen($_SESSION['session_image'], 'latin1'));
The image content is changed, may be due to encoding type. When printing the session stored data back to the client some characters have disappeared. This may have affect due to Drupal session handling as it stores session in the database.
The solution was to overcome all the issues was to base64_encode and store. Then retrieve the content as base64 encoded source via an ajax call.
The code is :
print ' img_src = "data:image/*;base64,". $_SESSION['session_image'] . '"';
In the javascript code set the img_src
value to the img
field src.
Edited: Root cause found. It was some of module and related files uploaded to the server has BOM(Byte Order Mark) characters at the beginning of files and it causes some time to have BOM characters for UTF-8 encryption. Having BOM for a UTF-8 stream is not recomended and some of the browsers are reporting rendering and content related issues especially in IE and Chrome.