Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"did not select a file to upload" when uploading using codeigniter

im trying to upload an image but it always give me "You did not select a file to upload."

My controller

function add()
{

        $thedate=date('Y/n/j h:i:s');
        $replace = array(":"," ","/");
        $newname=str_ireplace($replace, "-", $thedate);

        $config['upload_path'] = './upload/';
        $config['allowed_types'] = 'gif|jpg|png|jpeg';
        $config['file_name']=$newname;
        $config['max_size'] = '100';
        $config['max_width']  = '1024';
        $config['max_height']  = '768';

        $this->load->library('upload', $config);
        //$this->upload->initialize($config);
        $this->load->library('form_validation');

        $this->form_validation->set_rules('title','title','trim|required');
        $this->form_validation->set_rules('description','Description','trim|required');
        $image1=$this->input->post('image');


     if ($this->form_validation->run()==FALSE){

            $this->addview();   

            return false;

        }


      if (!$this->upload->do_upload($image1)) {

        $error = array('error' => $this->upload->display_errors());
        $this->load->view('upload_error', $error);


         }

       else {
        $mage=$this->upload->do_upload($image1);

            $data =array(
            'title'=>$this->input->post('title'),
            'descrip'=>$this->input->post('description'),

            'image' => $mage['file_name']

    );  


            $this->load->model('member_functions');

            $q=$this->member_functions->insert($data);
    }}

all the file requirements and the file permissions are set but i still get the that eror. can someone please tell me what am i doing wrong

like image 415
LiveEn Avatar asked Sep 17 '11 19:09

LiveEn


People also ask

Which method is used to upload file in codeigniter?

File uploading in CodeIgniter has two primary parts—the frontend and the backend. The frontend is taken care of by the HTML form that uses the form input type file. On the backend, the file upload library forms the submitted input from the form and writes it to the transfer registry.

How do I allow all files in codeigniter?

Upload any File – Codeigniter$this->upload->set_allowed_types('*'); Call the upload library first by using this->load->library('upload'). set allowed type to * will make any file upload in codeigniter.


4 Answers

Parameter to $this->upload->do_upload() function should be the name of the form field. (If you call it without parameters userfile would be used). It seems in your case it should be 'image'. Instead of:

$image1=$this->input->post('image');

it should be:

$image1='image';
like image 177
cenanozen Avatar answered Sep 21 '22 16:09

cenanozen


  • View page

form tag should contain enctype="multipart/form-data".
ie, <form action='' method=''enctype="multipart/form-data> <input type='file' name='field_name'/>

  • controller

and in controller upload code should be $this->upload->do_upload("field_name")
and jsut check whether file reaches in server side by making just print like

print_r($_FILES);

if you get null array then make sure that client side code is correct.

like image 25
shihabudheen Avatar answered Sep 19 '22 16:09

shihabudheen


I totally agree with the advice about making sure the name of the file element in the form is either userfile or, if different, is passed to the do_upload method. However, for future reference it's also worth noting this line:

$image1=$this->input->post('image');

comes before

if (!$this->upload->do_upload($image1)) {

I found the post method of the input class does not return anything until after the do_upload method is called. Also, you've already called it in the if statement so you don't need to call it again in the else clause. After calling do_upload you now have access to form elements with $this->input->post and information about the uploaded file with $this->upload->data()

like image 40
DavidHyogo Avatar answered Sep 21 '22 16:09

DavidHyogo


make sure you use

form_open_multipart()

if you are using the form helper.

like image 42
etlds Avatar answered Sep 21 '22 16:09

etlds