在codeigniter中上传多个文件,并使用数组重命名每个文件

时间:2018-09-27 09:30:23

标签: php codeigniter

我有多个上载控制器,并且工作正常,但是我想使用一个控制器上载时重命名每个文件。

当我单击“提交”时,我想要的是第一个文件名为IKPI,第二个文件名为NPWP

<div class="col-sm-6">
    <div class="alert alert-block alert-danger">
        <p>
            <b>IKPI</b>
        </p>
        <p>
            <div class="col-xs-12">
                <input type="file" id="id-input-file-2" name="multipartFiles[]" />
            </div>

            <input type="submit" name="submit" value="Upload File" class="btn btn-sm btn-primary">
        </p>
    </div>
</div>
<div class="col-sm-6">
    <div class="alert alert-block alert-danger">
        <p>
            <b>NPWP</b>
        </p>
        <p>
            <div class="col-xs-12">
                <input type="file" id="id-input-file-2" name="multipartFiles[]" />
            </div>

            <input type="submit" name="submit" value="Upload File" class="btn btn-sm btn-primary">
        </p>
    </div>
</div>
  

在我的控制器中,我使用循环

for ($i=0; $i<$number_of_files ; $i++) { 
$_FILES['multipartFiles']['name'] = $files['multipartFiles']['name'] [$i];

然后上传

1 个答案:

答案 0 :(得分:0)

您可以尝试这样

         $file_names=['IKPI','NPWP']//save what filename would you want to give
           for($i = 0; $i < $number_of_files; $i++){
            $_FILES['file']['name']     = $file_names[$i];
            $_FILES['file']['type']     = $_FILES['multipartFiles']['type'][$i];
            $_FILES['file']['tmp_name'] = $_FILES['multipartFiles']['tmp_name'][$i];
            $_FILES['file']['error']     = $_FILES['multipartFiles']['error'][$i];
            $_FILES['file']['size']     = $_FILES['multipartFiles']['size'][$i];

            // File upload configuration
            $uploadPath = 'uploads/files/';
            $config['upload_path'] = $uploadPath;
            $config['allowed_types'] = 'jpg|jpeg|png|gif';

            // Load and initialize upload library
            $this->load->library('upload', $config);
            $this->upload->initialize($config);

            // Upload file to server
            if($this->upload->do_upload('file')){
                // Uploaded file data
                $fileData = $this->upload->data();
                $uploadData[$i]['file_name'] = $fileData['file_name'];
                $uploadData[$i]['uploaded_on'] = date("Y-m-d H:i:s");
            }
        }