微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

PHP-无法在Codeigniter中从1个表单保存2个文件

我有一个表格,要求用户上传2个文件,文件上传& imgupload.我正在尝试通过ajax保存整个表单.

表单和Ajax代码

<form enctype="multipart/form-data" id="data">
  <input type="file" id="file-upload" class="file-upload" name="file-upload" >
  <input type="file" id="imgupload" class="imgupload" name="imgupload"  >

  <button type="submit" id="save" class="save-icon-btn">
    <img src="<?PHP echo base_url(); ?>assets/img/Save.png">
  </button>
</form>

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

控制器代码

$config['upload_path'] = './assets/file/.';
$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('file-upload')) 
  {
    $error = array('error' => $this->upload->display_errors());
     print_r($error);
  }
else
  {
    $data = $this->upload->data();
    echo $res = $data['file_name'];
  }


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

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

if (!$this->upload->do_upload('imgupload')) 
  {
    $error1 = array('error' => $this->upload->display_errors());
    print_r($error1);
  }
else
  {
    $data1 = $this->upload->data();
    echo $res1 = $data1['file_name'];
  }

问题在于,在控制器代码中,只有1个文件上传.如果我将文件上传作为第一个上传代码,则仅将其上传而imgupload将不上传.

如果我将imgupload保留为第一个上传代码,则仅此将被上传,而文件上传将不会被上传.

我不明白为什么会这样.任何人都可以告诉这个问题的解决方

解决方法:

请尝试此更新的功能.您正在做一些奇怪的事情,例如初始化第一个而不是第二个.由于CI是单例,因此可能会导致某些问题(不是说这是根本原因).

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

        echo '<pre>';
        print_r($_FILES);

        $config['upload_path'] = './assets/file/.';
        $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('file-upload')) {
            $error = array('error' => $this->upload->display_errors());
            print_r($error);
        } else {
            $data = $this->upload->data();
            echo $res = $data['file_name'];
        }

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

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

        if (!$this->upload->do_upload('imgupload')) {
            $error1 = array('error' => $this->upload->display_errors());
            print_r($error1);
        } else {
            $data1 = $this->upload->data();
            echo $res1 = $data1['file_name'];
        }

如果不起作用.请让我们知道$_FILES数组正在打印什么.

更新:

由于$this-> load-> library(‘upload’,$config2);,此操作无效.当CI看到加载时,它将首先检查是否加载了该类,而与传递的参数无关,因为已经加载了该行,因此忽略了该行,并使用了$config(第一个图像上传)中的设置.

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐