• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Upload multiple files and displaying files

#1
My web app only allows for one file upload which is an issue because users want to be able to submit 2 or more files when necessary. Please how do I allow multiple file uploads and displaying those files to be downloaded by another type of users?

Controller

Code:
public function upload_docs()
    {

        $homework_id         = $_REQUEST['homework_id'];
        $student_id          =$_REQUEST['student_id'];
        $data['homework_id'] = $homework_id;
        $data['student_id']  = $student_id;
        $data['message']     = $_REQUEST['message'];
        // $data['id']=$_POST['assigment_id'];
         $is_required=$this->homework_model->check_assignment($homework_id,$student_id);
          $this->form_validation->set_rules('message', $this->lang->line('message'), 'trim|required|xss_clean');

  $this->form_validation->set_rules('file', $this->lang->line('attach_document'), 'trim|xss_clean|callback_handle_upload['.$is_required.']');

        if ($this->form_validation->run() == FALSE) {
          $msg=array(
            'message'=>form_error('message'),
            'file'=>form_error('file'),
          );
          $array = array('status' => 'fail', 'error' => $msg, 'message' => '');

        }else{

            if (isset($_FILES["file"])){
            foreach($_FILES["file"] as $file){
            if(!empty($file["name"])){
                $time     = md5($file["file"]['name'] . microtime());
                $fileInfo = pathinfo($file["file"]["name"]);
                $img_name = $time . '.' . $fileInfo['extension'];           
            $data['docs'] =  $img_name;
            move_uploaded_file($file["file"]["tmp_name"], "./uploads/homework/assignment/" . $data['docs']);

            $data['file_name']=$file["file"]['name'];

            $this->homework_model->upload_docs($data);
        }

         $array = array('status' => 'success', 'error' => '', 'message' => $this->lang->line('success_message'));
        }

        echo json_encode($array);
    }
    }
    }

    public function handle_upload($str,$is_required)
    {

        $image_validate = $this->config->item('file_validate');

        if (isset($_FILES["file"]) && !empty($_FILES['file']['name']) && $_FILES["file"]["size"] > 0) {

            $file_type         = $_FILES["file"]['type'];
            $file_size         = $_FILES["file"]["size"];
            $file_name         = $_FILES["file"]["name"];
            $allowed_extension = $image_validate['allowed_extension'];
            $ext               = pathinfo($file_name, PATHINFO_EXTENSION);



            $allowed_mime_type = $image_validate['allowed_mime_type'];

            $finfo = finfo_open(FILEINFO_MIME_TYPE);
            $mtype = finfo_file($finfo, $_FILES['file']['tmp_name']);
            finfo_close($finfo);

            if (!in_array($mtype, $allowed_mime_type)) {
                $this->form_validation->set_message('handle_upload', 'File Type Not Allowed');
                return false;
            }

            if (!in_array($ext, $allowed_extension) || !in_array($file_type, $allowed_mime_type)) {
                $this->form_validation->set_message('handle_upload', 'Extension Not Allowed');
                return false;
            }

            if ($file_size > $image_validate['upload_size']) {
                $this->form_validation->set_message('handle_upload', $this->lang->line('file_size_shoud_be_less_than') . number_format($image_validate['upload_size'] / 1048576, 2) . " MB");
                return false;
            }

            return true;
        } else {
          if($is_required==0){
             $this->form_validation->set_message('handle_upload', 'Please choose a file to upload.');
            return false;
          }else{
             return true;
          }

        }


    }


View


Code:
<form id="upload" role="form" method="post" class="ptt10" enctype="multipart/form-data" action="uploaded_docs">
<input type="file"  multiple=""  id="file" name="file[]" class="form-control filestyle">
<button type="submit" form="upload" class="btn btn-info pull-right" id="submit" data-loading-text=' Please wait'><?php echo $this->lang->line('save'); ?></button>
</form>
Reply

#2
You can do it like this:
https://gist.github.com/zitoloco/1558423
Reply

#3
(05-16-2020, 10:09 AM)jreklund Wrote: You can do it like this:
https://gist.github.com/zitoloco/1558423

I'm afraid I don't have real knowledge of php codeigniter. Could you help me with this?
Reply

#4
Sorry, I don't have the time to understand and re-write your controller to support multiple uploads.
Reply


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.