I am new to php & mysql I was trying to to save user image to the MySql datbase. I have seen this solution on php.net
<?php
$data = file_get_contents('path/to/image.jpg');
$data = base64_encode($data);
//after reading data would be like this
$data = 'iVBORw0KGgoAAAANSUhEUgAAABwAAAASCAMAAAB/2U7WAAAABl'
. 'BMVEUAAAD///+l2Z/dAAAASUlEQVR4XqWQUQoAIAxC2/0vXZDr'
. 'EX4IJTRkb7lobNUStXsB0jIXIAMSsQnWlsV+wULF4Avk9fLq2r'
. '8a5HSE35Q3eO2XP1A1wQkZSgETvDtKdQAAAABJRU5ErkJggg==';
$data = base64_decode($data);
$im = imagecreatefromstring($data);
if ($im !== false) {
header('Content-Type: image/png');
imagepng($im);
imagedestroy($im);
}
else {
echo 'An error occurred.';
}
?>
it works but i am not getting how to store value of '$data' in MySql database. Should I take varchar datatype for my field and what would be the maximum limit of varchar??
Is this correct way or there are better ways then this if yes then what are they??