dongniaoli1822 2013-10-04 12:44
浏览 43

将图像文件保存在MySql中

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??

  • 写回答

1条回答 默认 最新

  • douaoli5328 2013-10-04 13:03
    关注

    You could use text. Then you don't need to define maximum length. since the base64 encoded string could be quite long i its a big image.

    An other solution would be to store the image path instead.

    评论

报告相同问题?

悬赏问题

  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大