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:
- Store the uploaded file in temporary location and serve it.
- Change the content type to another
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.