user6746423
user6746423

Reputation:

Upload file and save its path in database using codeingiter

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

Upvotes: 1

Views: 62

Answers (1)

Mayank Pandeyz
Mayank Pandeyz

Reputation: 26258

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

Upvotes: 1

Related Questions