Welcome Guest, Not a member yet? Register   Sign In
Upload Attachment Functionality Help Reqd
#1

(This post was last modified: 06-14-2016, 07:47 AM by Aneri.)

Hello,

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.

VIEW

<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){

        $error='';
       //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']);
         $total_size=0;
          //save total file size limit
         for($c=0;$c<$cpt;$c++){
                $total_size=$total_size+$files['userfile']['size'][$c];
         }
         //echo 'Total size : '.$total_size;
         //check total file size limit
         if($total_size>(1024*1024*5)){
             //set error message
             $this->session->set_flashdata('fileupload_error','Total file size limit reached.');
             //if error then reload case cerate page
             redirect(site_url('service/create_case_details'),'refresh');
         }else{
          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
               $error.=$this->upload->display_errors();
               $this->session->set_flashdata('fileupload_error',$error);
               //if error then reload case cerate page
               //redirect(site_url('service/create_case_details'),'refresh');
           }
           else {
              //collect data for db   
              $file=$this->upload->data();
              $doc['case_id']=$case_id;
              $doc['doc_name']=$file['file_name'];
              $doc['doc_mymetype']=$file['file_type'];
              $doc['doc_size']=$file['file_size'];
              $doc['doc_path']=$config['upload_url'].$file['file_name'];
              
              //call model function to save data
              $this->CI->service_model->save_document($doc);
              
           }
         }
         }
    }

And the DAO uploads to DB
  

    public function save_document($param=array()){
        $this->db->insert($this->document_table,$param);
        return $this->db->insert_id();
    }
Reply
#2

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

if(count($_FILES['userfile']['name'])>0)

Thats it.
Reply




Theme © iAndrew 2016 - Forum software by © MyBB