在 CodeIgniter 中上传多个文件

2022-08-31 00:51:23

在我的CodeIgniter项目中,我在项目创建期间上传文件。这是上传功能:

function uploadFiles(){

     $this->load->library('upload');  
     $error = 0;    
     $projectName = $_POST['projectname'];
     mkdir('./uploads/'.$projectName);

     for($i=0; $i<count($_FILES); $i++)
     {

       $_FILES['userfile']['name']    = $_FILES['files']['name'][$i];
       $_FILES['userfile']['type']    = $_FILES['files']['type'][$i];
       $_FILES['userfile']['tmp_name'] = $_FILES['files']['tmp_name'][$i];
       $_FILES['userfile']['error']       = $_FILES['files']['error'][$i];
       $_FILES['userfile']['size']    = $_FILES['files']['size'][$i];

       $config['upload_path']   = './uploads/'.$projectName;
       $config['allowed_types'] = 'xml|etl';
       $config['max_size']      = '0';
       $config['overwrite']     = TRUE;

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

      if($this->upload->do_upload())
      {
        $error += 0;
      }else{
        $error += 1;
      }
     }

     if($error > 0){ return FALSE; }else{ return TRUE; }

}

我在create_project函数中调用此函数,如下所示:

public function create_project() {
    $this->load->library('form_validation');

    $this->form_validation->set_rules('projectname', 'Project name', 'required');

    $this->form_validation->set_message('required', 'This is an obligatory field');

    if($this->form_validation->run() == FALSE) {
        $this->load->view('project_creation_view');
    } else {
        $this->load->model('Project_management_model');
        $this->Project_management_model->create_project();
        $this->uploadFiles();
    }
}

但是,这不会做任何事情。文件未上载。甚至没有创建空目录。任何人都可以帮我找到错误吗?

谢谢。


答案 1

您可以上传任意数量的文件。

$config['upload_path'] = 'upload/Main_category_product/';
$path=$config['upload_path'];
$config['allowed_types'] = 'gif|jpg|jpeg|png';
$config['max_size'] = '1024';
$config['max_width'] = '1920';
$config['max_height'] = '1280';
$this->load->library('upload', $config);

foreach ($_FILES as $fieldname => $fileObject)  //fieldname is the form field name
{
    if (!empty($fileObject['name']))
    {
        $this->upload->initialize($config);
        if (!$this->upload->do_upload($fieldname))
        {
            $errors = $this->upload->display_errors();
            flashMsg($errors);
        }
        else
        {
             // Code After Files Upload Success GOES HERE
        }
    }
}

答案 2

这是我修改为上传多个文件的CI_Upload类的扩展,只需将其复制到MY_Upload.php文件即可。它还会创建目录。

http://pastebin.com/g9J6RxW1

然后,您需要在控制器函数中做的就是将文件排列到数组中,其中字段的名称和键是数组键,配置是数据。在你的情况下,像这样的东西:

$project_files['files'][0] = array(
            'upload_path' => './uploads/'.$projectName.'/',
            'max_size' => 0,
            'allowed_types' => 'xml|etl',
            'overwrite' => TRUE,
            'remove_spaces' => TRUE,
        );
$project_files['files'][1] = array(
            'upload_path' => './uploads/'.$projectName.'/',
            'max_size' => 0,
            'allowed_types' => 'xml|etl',
            'overwrite' => TRUE,
            'remove_spaces' => TRUE,
        );

如果所有文件配置都相同,只需创建一个for循环来设置它,它也将采用“命名键”,即。.$project_files['files']['file_key']

然后只需调用:

 if($this->upload->upload_files($project_files)){/*all files uploaded successfully*/}

推荐