codeigniterfile-uploadcodeigniter-form-helper

Codeigniter File Upload Not Functioning


I'm having problems trying to implement the Codeigniter class in my application. The issue is that when I submit my form, the function that executes is not saving the file to my specified folder and it is not throwing an error message. My code is as follows:

View:

<?php echo form_open_multipart('requests/submit_candidate'); ?>
<table>
        <tr>
        <td><label for="text">Upload CV</label></td>
        <td><input type="file" name="file"></textarea></td>
    </tr>
    <tr>
        <td><label for="text">Extra Information</label></td>
        <td><textarea name="extra_info"></textarea></td>
    </tr>       
</table>
<input type="submit" name="submit" value="Submit" />
</form>

Controller:

public function submit_candidate($slug)
    {
        $this->load->library('upload');

        $config =  array(
              'upload_path'     => dirname($_SERVER["SCRIPT_FILENAME"])."/uploads/",
              'upload_url'      => base_url()."uploads/",
              'allowed_types'   => "gif|jpg|png|jpeg|pdf|doc|xml",
              'overwrite'       => TRUE,
              'max_size'        => "1000KB",
              'max_height'      => "768",
              'max_width'       => "1024"  
            );

        if($this->upload->do_upload($config))
        {
            echo "file upload success";
        }
        else
        {
           echo "file upload failed";
        }
}

Can anybody help please?

Many thanks,

SR


Solution

  • Try this if it works for you:

    1) HTML

    <td><input type="file" name="file"></td>
    

    2) Controller

    public function submit_candidate($slug)
    {
        $this->load->library('upload');
    
        $config =  array(
              'upload_path'     => "./uploads/",
              //'upload_url'      => base_url()."uploads/",
              'allowed_types'   => "gif|jpg|png|jpeg|pdf|doc|xml",
              'overwrite'       => TRUE,
              'max_size'        => 1000,
              'max_height'      => 768,
              'max_width'       => 1024  
            );
        $this->upload->initialize( $config );
        if(!$this->upload->do_upload('file'))  // "file" is the name of the element default it's "userfile"
        {
            print_r( $this->upload->display_errors() );die; //display the error
        }
        else
        {
           $data    = $this->upload->data();
           print_r( $data );die;                            //file uploaded data
        }
    }
    

    1) Inside the function you need to give the name of the form file element ( for you its "file" )
    2) You have also used a config variable "upload_url" which is invalid.
    3) You need to initialize the config for upload library.
    Here (File Upload Documentation) is the reference for you.