Sunday 15 July 2012

php - how to store and retrieve images in database using CodeIgniter -


I am using a WAMP server, and I want to upload images using CII in the database. Blob datatype my question is as follows:

1) How to store the image instead of the file name and what datat should I use?

2) How to recover images from DB?

My Controller's Code:

  & lt ;? Php Class Image_control extends CI_Controller {function index () {// $ this-> Load-> View ('image_view'); // $ this- & gt; Image_model-> Do_upload (); $ Data ['images'] = $ this- & gt; Image_model-> Get_images (); $ This- & gt; Load-> View ('image_view', $ data); } Function do_upload () {$ config = array ('allowed_types' =>; jpg | png | bmp '', 'upload_path' = & gt; '. / Images1 /', 'max_size' => 2000); $ This- & gt; Load-> Library ('upload', $ config); If (! $ This-> upload-> do_upload ()) {$ errors [] = array ('error' =>> $ this-> upload-> display_rursor ()); $ This- & gt; Load-> View ('image_view', $ errors); } $ Image_path = $ this- & gt; Upload-> Data (); $ File_name = $ IMAGE_PATH ['file_name']; $ Config = array ('a_name' = & gt; $ this-> Input- & gt; Post ('a_name'), 'a_details' => $ this-> Input-> Post ( 'A_info'), 'A_photo' = & gt; $ file_name); $ Insert = $ this- & gt; Db- & gt; Insert ('animalstore', $ config); Enter Refund $; }}? & Gt;   

Code of my model:

   num_rows> 0) {foreach ($ query-> result () as $ rows} {$ data [] = $ rows; } Return $ data; }}}? & Gt;   

And finally here is the code for my view:

  & lt; Php echo form_open_multipart ('image_control / do_upload'); Echo form_input ('a_name', 'animal name'); Echo form_input ('a_info', 'animal notice'); Echo form_upload ('userfile'); Eqow form_submit ('upload', 'upload'); Echo form_close (); ? & Gt; & Lt ;? Php foreach ($ Picture $ as image):? & Gt; & Lt; H1 & gt; & Lt ;? Php echo $ image- & gt; a name;? & Gt; & Lt; / H1> & Lt; H1 & gt; & Lt ;? Php echo $ image- & gt; A_details ;? & Gt; & Lt; / H1> & Lt; Img src = "/" & lt; php // echo ltrim ($ image-> one_photo, '/');? & Gt; " & Gt; & Lt; Img src = "http: // localhost / ci_test/images1/" lt; php echo $ image-> a_photo ;? & gt; " /> & Lt; Img src = "& lt ;? php // echo sprintf (" image /% s ", $ image ['screenshot']) ;? & gt;" / & Gt; & Lt; H1 & gt; & Lt ;? Php // echo $ image- & gt; A Photo;? & Gt; & Lt; / H1> & Lt ;? Php endforeach; ? & Gt;   

I tried to solve it in different ways and searched my problem, but I could not find any suitable answer.

Do not deposit files inside the database !!!

It is always a poorly designed idea to store files in the file system and store file names and point to the file, this will give you many headaches in the future.

No comments:

Post a Comment