无法以codeigniter格式上传文件

时间:2018-03-09 05:18:39

标签: php ajax codeigniter file-upload upload

我有一个允许用户输入数据和上传文件的表单

<form enctype="multipart/form-data" id="form">
  <input type="file" id="fileImage" class="fileImage" name="fileImage" >
  <button type="submit" id="save" class="save-icon-btn">
    Submit
  </button>
</form>

Ajax代码

$("#save").click(function()
  {
    var form_data = new FormData($('#form')[0]);
    jQuery.ajax(
      {
        type: "POST",
        url: "<?php echo base_url(); ?>" + "class/student",
        data: form_data,
        processData: false,
        contentType: false,
        success: function(res) 
          {
            console.log(res);
            alert(res);
          }
      });
  });

后端代码

$config['upload_path'] = './assets/student/.';
$config['allowed_types'] = 'gif|jpg|png|doc|txt';
$config['max_size'] = 1024 * 8;
$config['encrypt_name'] = TRUE;
$this->load->library('upload', $config);
$this->upload->initialize($config);

if (!$this->upload->do_upload('fileImage')) 
    {
      $error = array('error' => $this->upload->display_errors());
       print_r($error);
    }
else
    {
      $data = $this->upload->data();
    }

我希望再添加一个上传功能,允许用户上传一个单独的文件,但是在同一个表单中。所以新表格应该是

<form enctype="multipart/form-data" id="form">
  <input type="file" id="fileImage" class="fileImage" name="fileImage" >
  <input type="file" id="fileReport" class="fileReport" name="fileReport"  >
  <button type="submit" id="save" class="save-icon-btn">
    Submit
  </button>
</form>

对于后端,我将以下代码添加到现有代码

$config1['upload_path'] = './assets/report/.';
$config1['allowed_types'] = 'gif|jpg|png|doc|txt';
$config1['max_size'] = 1024 * 8;
$config1['encrypt_name'] = TRUE;
$this->load->library('upload', $config1);
$this->upload->initialize($config1);

if (!$this->upload->do_upload('fileReport')) 
    {
      $error = array('error' => $this->upload->display_errors());
       print_r($error);
    }
else
    {
      $data = $this->upload->data();
    }

但是我无法理解我需要如何更改我的ajax代码以便它可以发送数据。

使用更新的表单和后端代码,我无法保存这两个文件。只上传了fileImage,而不是fileReport。

2 个答案:

答案 0 :(得分:0)

ajax代码:

var form_data = new FormData($('#form')[0]);
form_data.append('my_file3[]', $('input[type=file]')[0].files[0]); 
form_data.append('my_file3[]', $('input[type=file]')[1].files[0]); 
jQuery.ajax(
      {
        type: "POST",
        url: "<?php echo base_url(); ?>" + "class/student",
        data: form_data,
        async : false,
        cache : false,
        processData: false,
        contentType: false,
        success: function(res) 
          {
            console.log(res);
            alert(res);
          }
      });

在控制器代码中:

 $cpt = count ($_FILES['my_file3']['name']);
    $target_dir = 'assets/files';
    $this->common_lib->make_dir_if_not($target_dir);
    for($i = 0; $i < $cpt; $i ++) {

        $_FILES['images']['name'] = $_FILES['my_file3']['name'][$i];
        $_FILES['images']['type'] = $_FILES['my_file3']['type'][$i];
        $_FILES['images']['tmp_name'] = $_FILES['my_file3']['tmp_name'][$i];
        $_FILES['images']['error'] = $_FILES['my_file3']['error'][$i];
        $_FILES['images']['size'] = $_FILES['my_file3']['size'][$i];

        $config = $this->common_lib->set_upload_config($target_dir);

        $this->load->library('upload');
        $this->upload->initialize($config); 
        if($this->upload->do_upload("images")){
        $fileName = $_FILES['images']['name'];
        $images[] = $fileName;
        $my_file .= $fileName;
        }
    }

**我为常用功能制作了公共库(common_lib),如邮件设置,上传设置,制作目录等。您可以使用自定义配置。

答案 1 :(得分:0)

请尝试以下代码。如果有效,我会详细说明。

print_r($_FILES); // show me this if it doesn't work

$this->load->library('upload');

$config['upload_path'] = './assets/student/.';
$config['allowed_types'] = 'gif|jpg|png|doc|txt';
$config['max_size'] = 1024 * 8;
$config['encrypt_name'] = TRUE;

$this->upload->initialize($config);

if (!$this->upload->do_upload('fileImage')) 
    {
      $error = array('error' => $this->upload->display_errors());
       print_r($error);
    }
else
    {
      $data = $this->upload->data();
    }

$config1['upload_path'] = './assets/report/.';
$config1['allowed_types'] = 'gif|jpg|png|doc|txt';
$config1['max_size'] = 1024 * 8;
$config1['encrypt_name'] = TRUE;

$this->upload->initialize($config1, true);

if (!$this->upload->do_upload('fileReport')) 
    {
      $error = array('error' => $this->upload->display_errors());
       print_r($error);
    }
else
    {
      $data = $this->upload->data();
    }