简体   繁体   中英

unable to upload image using codeigniter

Please help me. This is my first project in php Codeigniter. Actually i am a Java developer.

I want to upload two image path into my table and its image into my root folder eg uploads I have many fields in the same table other then images. I am able to add/edit those fields from my form.(i implemented succussfully using codeigniter)

But currently I am facing problem in image upload. I don't know exactly how to upload images using codeigniter. I tried to do it by myself since Two days but i could not solve my problem

Error : I am not seeing any error. simply it inserts 0 values into my db table as image path. I think the way i am try to upload image is not correct.

myviews.php

 <? echo form_open_multipart('Booksetups/book'); ?>


                 <input type="file" name="img1" /> 
                 <input type="file" name="img2" />
               <?php          
                                               <br/>
                          <? echo  form_submit($submitbtn);   echo form_reset($resetbtn); ?>  
                 <? echo form_close(); ?>  

First thing to remember is CI does not add $_FILES to the input object. You will need to access those like $_FILES['img1'] etc. So these:

'img1'=>$this->input->post('img1'),//image path is not inserting But all other fields are inserting into db 
'img2'=>$this->input->post('img2'),//image path is not inserting

should be something like:

'img1'=>$_FILES['img1']['name'],//image path is not inserting But all other fields are inserting into db 
'img2'=>$_FILES['img2']['name'],//image path is not inserting

depending on what you expect to be storing in the database. You can rename files, etc through the upload class. I would suggest reading over those docs.

Secondly, you don't appear to be calling the actual upload method:

$this->upload->do_upload()

Not sure if you needed this but... If you want multiple configs, you have to redefine the config for multiple files if you want them to have different paths...

$config['upload_path'] = 'uploads/'; 
$config['allowed_types'] = 'gif|jpg|jpeg|png'; 
$config['max_size'] = '1000'; 
$config['max_width'] = '1920'; 
$config['max_height'] = '1280';  
$this->load->library('upload', $config);
$this->upload->do_upload("img1");

$config['upload_path'] = 'some_other_dir/'; 
$config['allowed_types'] = 'gif|jpg|jpeg|png'; 
$config['max_size'] = '1000'; 
$config['max_width'] = '1920'; 
$config['max_height'] = '1280';  
$this->upload->initialize($config);
$this->upload->do_upload("img2");

and if you don't want them to have different paths, you can just load in the library as you do in your example and call do_upload() with no params passed.

If i missed the point or you need more info let me know and I may update.

Problem is you have use html in serverside. PHP doesn't know HTML but php. Client side or browser knows HTML.

<input type="file" name="img1" /> 
<input type="file" name="img2" />

Use suitable php method to generate html.

second thing, Your file upload parameter is different fromo codeigniter userguide

$config['upload_path'] = 'uploads/';
$config['upload_path'] = './uploads/'; 

wrong file path may cause your otherproblems

It seems that you are not initializing the upload class library:

$this->upload->initialize($config);

Also do you load the library:

$this->load->library('upload');

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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