CodeIgniter Forums

Full Version: Upload Attachment Functionality Help Reqd
You're currently viewing a stripped down version of our content. View the full version with proper formatting.

I am developing a web app. The config details of the same are as follows:

App server - EC2 , Redhat Linux
Website - Developed in PHP using MVC architecture based on CodeIgniter.

I have a form where I am taking details for User case. I want to allow the user to be able to upload multiple attachments till 5 mb.

1. Everytime a user selects a file I want to display name of the file and the file size. There should be a X button next tio it.
2. Lets say the user has uploade 2 files and they add up to be 4 MB and he has another file of 2 MB, the app wont allow becoz of the 5 MB limitation. So an error will be displayed
3. Now if user feels file1 and file3 are more important, user should be able to click on the X and delete the file 2.

Initially I am trying a simple upload without having to show the file names etc. So that I know the functionality works. But the code is giving some problem and no error. Kindly read below and let me know what I am doing wrong.


<button class="select_file">Upload documents (upto 5MB)</button>
 <input class="uploadFiles" id="uploadFiles" type="file" name="userfile[]" multiple />

The MODEL Processes the File Array
    public function upload_docs($case_id){

       //file upload configuration
         $config['upload_path']   = dirname($_SERVER["SCRIPT_FILENAME"]).'/uploads/';
         $config['upload_url']   = base_url().'uploads/';
         $config['allowed_types'] = 'jpeg|jpg|png|pdf|doc|docx';
         $config['remove_spaces'] = TRUE;
         $config['max_size'] = 0;
         $this->load->library('upload', $config);
         $files = $_FILES;
         $cpt = count($_FILES['userfile']['name']);
          //save total file size limit
         //echo 'Total size : '.$total_size;
         //check total file size limit
             //set error message
             $this->session->set_flashdata('fileupload_error','Total file size limit reached.');
             //if error then reload case cerate page
          for($i=0; $i<$cpt; $i++)
                $_FILES['userfile']['name']= $files['userfile']['name'][$i];
                $_FILES['userfile']['type']= $files['userfile']['type'][$i];
                $_FILES['userfile']['tmp_name']= $files['userfile']['tmp_name'][$i];
                $_FILES['userfile']['error']= $files['userfile']['error'][$i];
                $_FILES['userfile']['size']= $files['userfile']['size'][$i];
                //upload file : default function of CI
           if ( ! $this->upload->do_upload()) {
               //if file upload fails then set error message
               //if error then reload case cerate page
           else {
              //collect data for db   
              //call model function to save data

And the DAO uploads to DB

    public function save_document($param=array()){
        return $this->db->insert_id();
Thank you I have resolved this.

There was only a small error in the line in the model class on submit

I was checking  - if(count($_FILES['userfile']['name'])>1)

and I actually needed to check


Thats it.