首页 > 解决方案 > Codeigniter 使用输入数组键为每个字段上传多个文件

问题描述

我在这里有多个字段,其中包含多个文件上传的数组键名。我需要为每个字段上传多个文件,并将带有键的数组中的文件名传递给模型。

我真的很需要这个,希望有人能帮助我。谢谢。

这是我的领域的例子

//with 2 or more files
<input accept="application/pdf" name="vtc_file[0]" id="vtc_file0"  type="file" multiple/>
//with 2 or more files
<input accept="application/pdf" name="vtc_file[1]" id="vtc_file1"  type="file" multiple/>


<?php

$count = count($_FILES['vtc_file']['name']);
  for ($i=0; $i < $count; $i++)
  {
      foreach ($_FILES['vtc_file'] as $key1 => $value1)
      {
          foreach ($value1 as $key2 => $value2)
          {
              $files[$key2][$key1] = $value2;
          }
      }
  }
  $_FILES = $files;

  $document_config['upload_path'] = 'uploads/';  
  $document_config['allowed_types'] = 'pdf';
    $this->load->library('upload', $document_config);
    foreach ($_FILES as $fieldname => $fileObject)
    {
        if (!empty($fileObject['name']))
        {
            $this->upload->initialize($document_config);
            if (!$this->upload->do_upload($fieldname))
            {
                $errors = $this->upload->display_errors();
            }
            else
            {
                 $results = $this->models->save();
            }
        }
    }

标签: phpcodeigniterfile-uploadupload

解决方案


我为你做了一个演示,我已经解释了代码本身的步骤。希望对你有效。

看法

<form action="<?Php echo base_url('home/myfunc'); ?>" method="POST" enctype="multipart/form-data">

    <!-- You need to make an array of input fields(See {name}) -->
    <input accept="application/pdf" name="vtc_file[0][]" id="vtc_file0"  type="file" multiple/>
    <!-- If the usdr uploads only one file it will be stored like vtc_file[0][1] = 'filename.pdf' and so on.-->
    <br>
    <input accept="application/pdf" name="vtc_file[1][]" id="vtc_file1"  type="file" multiple/><br>
    <button type="submit" value="submit">Submit</button>
</form>

控制器

function myfunc(){

    // check if the $_FILES is not empty(your validation here) then perform these actions↓↓
    foreach ($_FILES['vtc_file']['name'] as $key1 => $value1){  // We only need to loop through all the input fields(vtc_file)

        foreach ($value1 as $key2 => $value2){  // loop through name of all the files uploaded

            // Make a new dummy element(userfile) with the file(vtc_file') details in it
            $_FILES['userfile']['name']     = $_FILES['vtc_file']['name'][$key1][$key2];
            $_FILES['userfile']['type']     = $_FILES['vtc_file']['type'][$key1][$key2];
            $_FILES['userfile']['tmp_name'] = $_FILES['vtc_file']['tmp_name'][$key1][$key2];
            $_FILES['userfile']['error']    = $_FILES['vtc_file']['error'][$key1][$key2];
            $_FILES['userfile']['size']     = $_FILES['vtc_file']['size'][$key1][$key2];

            $config['upload_path']      = './uploads/aaa'; // path to the folder 
            $config['allowed_types']    = 'pdf';
            $config['max_size']         = 1000;
            // $config['max_width']    = 1024;
            // $config['max_height']   = 768;

            $this->load->library('upload', $config);    // load the {upload} library
            $this->upload->initialize($config);         // initialize the library

            if (!$this->upload->do_upload("userfile")){ // upload the file(current)

                $data['errors'][$key1][$key2] = $this->upload->display_errors();    // if any error store them in {errors} variable with keys

            }else{

                $upload_data = $this->upload->data();   // get the uploaded file data
                $data['filename'][$key1][$key2] = $upload_data['file_name'];    // store the upload file name in {filename} variable with keys
            }
        }
    }
    //load model {event_model}
    $this->load->model('event_model');
    $success = $this->event_model->save_file($data['filename']); // call model function

    // check if query successful
    if($success){
        // do something (Load a view)
        // You can show uploaded files in your view with $data['filename'][$key1][$key2]
        // And the errors of files that couldn't be uploaded with $data['errors'][$key1][$key2]
        echo '<pre>'; print_r($data['errors']);
    }else{
        // do something else
    }
}

模型

function save_file($data){

    echo '<pre>'; print_r($data);
    // Your insert query here.
    return true;
}

输出:

模型: $filename array

Array
(
    [0] => Array
        (
            [0] => Account_Software______Payment.pdf
            [1] => Account_Software______RTGS_Report.pdf
        )

    [1] => Array
        (
            [0] => BUSINESS_PROFILE.pdf
        )

)

控制器$errors array

Array
(
     [1] => Array
         (
              [1] => 
        The filetype you are attempting to upload is not allowed.


          )

)

图片:
在此处输入图像描述


推荐阅读