如何在CodeIgniter中上传多个文件?

时间:2015-10-02 04:52:17

标签: php codeigniter upload multifile-uploader

我必须上传多个文件。但是有些问题,请选择文件并提交不发生更改,也不要转到我的控制器函数multiple_upload

脚本

 <script type="text/javascript">
      var config = {
    // Valid file formats
          support : "image/jpg,image/png,image/bmp,image/jpeg,image/gif,application/vnd.ms-excel,application/vnd.openxmlformats-officedocument.spreadsheetml.sheet,text/csv,application/pdf", 
          form: "demoFiler", // Form ID
          dragArea: "dragAndDropFiles", // Upload Area ID
          uploadUrl: "<?php echo base_url(); ?>home/multiple_upload"     // Server side file url
      }
   //Initiate file uploader.
     $(document).ready(function()
    {
        initMultiUploader(config);
    });
</script>

查看

 <div id="dragAndDropFiles" class="uploadArea">
       <h1>Drop Your Files Here</h1>
 </div>
 <input type="file" name="multiUpload" id="multiUpload" multiple />
 <div class="progressBar">
      <div class="status"></div>
 </div>
 <button type="submit" class="btn blue" name="submitHandler" id="submitHandler" style="margin-top: 15px;margin-left: 54px;">Submit</button>

控制器

 function multiple_upload()
{
     $application_id=$this->session->userdata('application_id');
     echo $application_id;
      if(!is_dir('./application/assets/acc_data/'.$application_id)) 
     {
        mkdir('./application/assets/acc_data/'.$application_id, 0777, TRUE);

     }
}

1 个答案:

答案 0 :(得分:2)

试试这个

<html>
<body>
<form method="POST" action="<?php echo site_url('my-controller/file_upload');?>" 'enctype'=>'multipart/form-data'>
<label for="file">Filename:</label>
<input type="file" name="userfile[]" id="file" multiple>
<input type="submit" value="upload"></form>
</body>
</html>

并在你的控制器中创建一个函数

 $files = $_FILES;
                $cpt = count($_FILES['userfile']['name']);
                 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];
                $this->upload->initialize($this->set_upload_options());
                $this->upload->do_upload();
                $fileName = $_FILES['userfile']['name'];
                 $images[] = $fileName;

有关如何使用codeigniter try this

上传多个文件的更多信息