1

概述

我有一个包含四个文件上传字段的多部分表单,其中三个是动态添加的(使用 JavaScript)。

<input type="file" name="OneCertificate" />

<input type="file" id="MultipleCertificate[1] "name="MultipleCertificate[]" />
<input type="file" id="MultipleCertificate[2] "name="MultipleCertificate[]" />
// more if Add button is pressed 

这是输出var_dump($_FILES)

["OneCertificate"]=> array(5) {
        ["name"]=> string(6) "DE.pdf"
        ["type"]=> string(15) "application/pdf"
        ["tmp_name"]=> string(24) "C:\xampp\tmp\php37C2.tmp"
        ["error"]=> int(0)
        ["size"]=> int(103845)
}
// **Notice the attributes are all in their own arrays**
["MultipleCertificate"]=> array(5) { 
    ["name"]=> array(2) { [0]=> string(6) "DE.pdf" [1]=> string(6) "DE.pdf" } 
    ["type"]=> array(2) { [0]=> string(15) "application/pdf" [1]=> string(15) "application/pdf" } 
    ["tmp_name"]=> array(2) { [0]=> string(24) "C:\xampp\tmp\phpD941.tmp" [1]=> string(24) "C:\xampp\tmp\phpD942.tmp" } 
    ["error"]=> array(2) { [0]=> int(0) [1]=> int(0) } 
    ["size"]=> array(2) { [0]=> int(103845) [1]=> int(103845) } 
 }
 // and so on...

以下是我上传每个文件的方式:

function upload_file($field_name)
{
    // timestamp name: http://stackoverflow.com/questions/7457152/did-not-select-a-file-to-upload-when-uploading-using-codeigniter
    $the_date= date('Y/n/j h:i:s');
    $replace = array(":"," ","/");
    $new_name = str_ireplace($replace, "-", $the_date);

    $config['upload_path'] = './uploads/';
    $config['file_name'] = $new_name;
    $config['allowed_types'] = 'pdf|jpg|jpeg|png';

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

    // Get OneCertificate the normal way since it will only have one file content
    if ( $field_name == 'OneCertificate' ) {
        if ( ! $this->upload->do_upload($field_name)) {
            return array('error' => $this->upload->display_errors());
        } else {
            $file_data = array('upload_data' => $this->upload->data());
        }
    // Method for MultipleCertificate
    } else {
        for ($i = 0; $i < count($_FILES[$field_name]['name']); $i++) {
            if ( ! $this->upload->do_upload($_FILES[$field_name]['name'][$i])) {
                $file_data = array('error' => $this->upload->display_errors());
            } else {
                $file_data = array('upload_data' => $this->upload->data());
            }
         } // END for loop  
    }

    return $file_data;
 }

问题

我注意到OneCertificate作品的格式,因为它的所有信息都在一个数组中,而MultipleCertificate每个属性都在它自己的数组中。

第一个设法成功上传文件,但后者抛出你没有选择要上传的文件。

如何转换和/或检索MultipleCertificateinto的形式OneCertificate

注意:这是我需要的表格,因为我会将创建的数组分配给$OneCertificate数据库$MultipleCertificate插入。

4

1 回答 1

0

对于像我这样在如何$_FILES处理文件数组方面遇到问题的人,上面的hohner的评论解决了这个问题。

这是答案的SO链接。

基本上我的“循环到数组的内部$_FILE数组”是这样的:

function do_upload($file_name)
{
    $this->load->library('upload');
    $this->total_count_of_files = count($_FILES[$file_name]['name']);

    // timestamp name: https://stackoverflow.com/questions/7457152/did-not-select-a-file-to-upload-when-uploading-using-codeigniter
    $date_timestamp = date('Y/M/j h:i:s');
    $replace = array(":"," ","/");
    $new_name = $file_name.'-'.$this->session->userdata('AccreditNo').'-'.str_ireplace($replace, '', $date_timestamp);

    for($i = 0; $i < $this->total_count_of_files; $i++) {
        // userfile is arbitrary, it will not reflect on the final array
        $_FILES['userfile']['name']     = $_FILES[$file_name]['name'][$i];
        $_FILES['userfile']['type']     = $_FILES[$file_name]['type'][$i];
        $_FILES['userfile']['tmp_name'] = $_FILES[$file_name]['tmp_name'][$i];
        $_FILES['userfile']['error']    = $_FILES[$file_name]['error'][$i];
        $_FILES['userfile']['size']     = $_FILES[$file_name]['size'][$i];

        $config['file_name']     = $new_name.'-'.$i;
        $config['upload_path']   = "./uploads/$file_name";
        $config['allowed_types'] = 'pdf|jpg|jpeg|png';
        $config['max_size']      = '0';

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

        if ( ! $this->upload->do_upload()) {
            $file_data = array('error' => $this->upload->display_errors());
        } else {
            $file_data = array("file_{$i}" => $this->upload->data());
        }
    }

    return $file_data;
}   

文件数据现在将包含要插入数据库的一个文件的组合属性!(在这种情况下我希望它如何)

于 2013-02-17T12:48:41.623 回答