多表单上传:在codeigniter中单独获取每个文件名

时间:2015-03-08 11:49:02

标签: php codeigniter file-upload multiple-file-upload

我想上传一些文件。我使用codeigniter:

Html:

<input type="file" name="file1" />
<input type="file" name="file2" />

php:

$config['upload_path'] = './upload/';
$path = $config['upload_path'];
$config['allowed_types'] = 'gif|jpg|jpeg|png';
$config['encrypt_name'] = 'TRUE';
$this->load->library('upload', $config);

foreach ($_FILES as $key => $value) {
        if (!empty($value['tmp_name']) && $value['size'] > 0) {
            if (!$this->upload->do_upload($key)) {
               // some errors
            } else {
                // Code After Files Upload Success GOES HERE
                $data_name = $this->upload->data();
                echo $data_name['file_name'];
            }
        }
    }

当我想echo文件名时,我会收到1.jpg2.jpg。但我希望将它们分开存放并插入数据库。

我该怎么做?谢谢:))

1 个答案:

答案 0 :(得分:3)

$data_name['file_name']中的值添加到数组中,并在foreach循环后执行insert_batch。

类似的东西:

$filename_arr = array();
foreach ($_FILES as $key => $value) {
        if (!empty($value['tmp_name']) && $value['size'] > 0) {
            if (!$this->upload->do_upload($key)) {
               // some errors
            } else {
                // Code After Files Upload Success GOES HERE
                $data_name = $this->upload->data();
                $filename_arr[] = $data_name['file_name'];
            }
        }
    }

$this->db->insert_batch('mytable', $filename_arr);