简体   繁体   English

如何使用Codeigniter将图像插入数据库

[英]How to insert image into database using codeigniter

I want to insert image into database table say ad_images 我想将图像插入数据库表说ad_images
my table structure is 我的表结构是
tblName : ad_images ad_imagesad_images

Image_ID int PK 
Image LONGBLOB
Ad_ID int FK
SubCat_ID int FK

so how insert image into above table using codeigniter thanx in advance 那么如何预先使用codeigniter thanx将图像插入上表

You should avoid saving images and binary files to the database. 您应该避免将图像和二进制文件保存到数据库。 Just save the path to the file in the database. 只需将文件的路径保存在数据库中即可。

If you still want to to that: 如果您仍然想要这样做:

Base64 encode(to avoid escaping, that may harm your data) the image data and put it into the field, like you would do with any other data. Base64编码(以避免转义,这可能会损害您的数据)图像数据,并将其放入字段中,就像处理任何其他数据一样。

 <?php

 $pathToImage = '/abc/img.jpg';

 // not sure about codeigniter syntax, but you should understand whatis meant. 
$table->image =
base64_encode(file_get_contents($pathToImage)); 
$table->save();

Don't forget to decode the data on your way back. 不要忘记在返回途中解码数据。

Ref: 参考:

base64_encode base64_encode

base64_decode base64_decode

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM