【问题标题】:How to upload multipl file fiedls in codeigniter如何在codeigniter中上传多个文件字段
【发布时间】:2020-06-17 04:23:59
【问题描述】:

我有一个包含 4 个文件字段的表单,我想正确上传这些文件。

我的代码如下:

public function do_upload(){
    $config['upload_path']          = './uploads/';
    $config['allowed_types']        = 'gif|jpg|png';
    $config['max_size']             = 100;
    $config['max_width']            = 1024;
    $config['max_height']           = 768;

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

    if ( ! $this->upload->do_upload('userfile')) {
        $error = array('error' => $this->upload->display_errors());

        $this->load->view('upload_form', $error);
    } else {
        $data = array('upload_data' => $this->upload->data());

        $this->load->view('upload_success', $data);
    }
}

【问题讨论】:

  • 前几天我写了一个类似问题的答案;也许这会有所帮助 - stackoverflow.com/questions/62373664/…
  • 您对此有何疑问?您能否解释一下究竟在给定代码中的哪些问题,以及您尝试解决的问题?

标签: php html codeigniter


【解决方案1】:
private function fileUpload($name, $config = []){
     $config['upload_path']          = './uploads/';
     $config['encrypt_name']         = TRUE;
     $config['file_ext_tolower']     = TRUE;
     $config['allowed_types']        = 'jpg|jpeg|png';
     $this->upload->initialize($config);
     if ( ! $this->upload->do_upload($name))
     {
         $error = array('error' => true, 'message' => $this->upload->display_errors());
         return $error;
     }
     else
     {
         return array('error' => false, 'upload_data' => $this->upload->data());
     }
}

在控制器中创建这个私有函数,当你必须上传带有文件字段名称的文件时调用这个函数,如果你想要一些其他配置,只需将配置数组作为下一个参数传递给

$data = $this->fileUpload('site_logo_dark')
if($data['error']){
  echo data['message']; // error message while uplaoding an file if have any
}else{
  echo $data['upload_data']['file_name']; // return you a name of the file which you just upload it you can save it to the database 
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-01-12
    • 2013-12-05
    • 1970-01-01
    • 2013-10-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多