1

I wish to upload a file to folder and then save its path in database, but i got stuck in first step only

whenever i am trying to upload a file i am getting a message

You did not select a file to upload

the code that i used is

view

<?php
    echo form_open_multipart('recruiter/adddata); 

       $data = array(
           'type'=>'file',
           'name'=>'userfile',
           'class'=>'fileinput btn-info',
           'id'=>'filename3',
           'data-filename-placement'=>'inside',
           'title'=>'Resume'
       );

       echo form_upload($data); 

        $data = array(
            'type'=>'submit',
            'class'=>'btn btn-danger',
            'name'=>'submit',
            'content'=>'Submit'

        );
        echo form_button($data); 

    echo form_close(); 
?>

Controller

$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'doc|docx|pdf';
$config['max_size'] = 10000;
$config['max_width'] = 1024;
$config['max_height'] = 768;

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

if ( ! $this->upload->do_upload('userfile'))
    {
        $error = array('error' => $this->upload->display_errors());
        print_r($error); //only for checking purposes
    }
else
    {
        $data = array('upload_data' => $this->upload->data());
        print_r($data); //only for checking purposes
    }

Can anyone please tell how can i upload the file and save its path in database

5
  • Change your echo form_open('recruiter/adddata); to echo form_open_multipart('recruiter/adddata'); I hope it will works. Commented Sep 1, 2016 at 5:50
  • @Kabir Hossain Not working Commented Sep 1, 2016 at 6:00
  • If not works just gives the error, then we will give you the next solution. Change if ( ! $this->upload->do_upload('userfile')) to $input_file= "userfile" // input name="userfile" if(! $this->upload->do_upload($input_file) ) Commented Sep 1, 2016 at 6:11
  • @Kabir Hossain tried but still i am getting same error. "You did not select a file to upload" Commented Sep 1, 2016 at 6:27
  • Change $config['allowed_types'] = 'doc|docx|pdf'; to $config['allowed_types'] = '*'; Commented Sep 1, 2016 at 6:29

1 Answer 1

1

Make some changes like:

echo form_upload($data); 

to

<?php echo form_open_multipart();?>

Controller function part:

//start of file upload code
$config['upload_path'] = './uploads/';
$config['allowed_types'] = '*';
$this->load->library('upload', $config);
$this->upload->do_upload();
$data = $this->upload->data();
//end of file upload code
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.