如何使用CodeIgniter编码多个文件上传?

时间:2014-11-10 09:15:36

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

我想要一个多文件上传代码。

例如:

Koala.jpg
Penguins.jpg
Jellyfish.jpg

有输入文本,用户可以在其中设置图像的新名称。

用户现在将上传图像,输入的新图像名称为#34;动物"

现在,我想要的是当上传输出时应该是Animals1.jpg,Animals2.jpg,Animals3.jpg。

问题是当我尝试上传所有这些图片时,只上传了一张图片。

我尝试进行研究并在我的程序中应用了一些代码,但仍然无效。

控制器

public function do_upload() { 
    $config = array(
        'image_library' => 'gd2',
        'file_name'     => $this->input->post('finame'),
        'upload_path'   => './public/img/uploads',
        'upload_url'    => base_url().'public/img/uploads',
        'allowed_types' => 'gif|jpg|jpeg',
        'max_size'      => '1024KB',
        'max_width'     => '1024',
        'max_height'    => '768',
        'maintain_ratio'=> TRUE,
        'overwrite'     => false,
    );
    $this->load->library('upload', $config);
    if (!$this->upload->do_upload()) {
        $error_msg = "<div class='alert alert-error'>".$this->upload->display_errors()."</div>";
        $error = array('error' => $error_msg);
    } 
    else {
        $upload_data = $this->upload->data();
        $data['thumbnail_name'] = $upload_data['raw_name']. '_thumb' .$upload_data['file_ext'];
        $file_array = array(
            'image'         => $data['thumbnail_name'],
            'image_name'    => $upload_data['file_name'],
            //'description'   => "",
            'date_created'  => date('Y-m-d H:i:s', now()),
            'date_modified' => date('Y-m-d H:i:s', now()),
            'author'        => $this->session->userdata('username'),
            'size'          => $upload_data['file_size'],
            'type'          => $upload_data['image_type'],
            'width'         => $upload_data['image_width'],
            'height'        => $upload_data['image_height'],
            //'document_name' => $field,
            //'department'    => $field2,
            //'notes'         => "",
        );
        $this->session->set_userdata('image_print', $file_array);
        $this->load->database();
        $this->db->insert('tbl_image', $file_array);
        $data = array('upload_data' => $this->upload->data());
        $user_level['records']=$this->user_model->get_records();
        $this->load->view('v_dashboard/page/header_view', $user_level);
        $this->load->view('v_document/upload/upload_result_view', $data);
        $this->load->view('v_dashboard/page/footer_view');
    }
}

我在HTML上有这个

<label for="file"><strong>Select File To Upload:</strong></label>
<input type="file" name="userfile[]" multiple class="btn transcolor btn-file"/>
<br/><br/>

顺便说一下,我在我的数据库上使用了BLOB。

我试图引用这个链接

Ellislab

GitHub

StackOverflow

StackOverflow

CodingLikeASir

1 个答案:

答案 0 :(得分:0)

您必须运行for循环,直到上传的图像文件的计数。

像这样:

for ($i=0; $i < count($_FILES['userfile']['name']); $i++)
{ 
  // function to add the image name one by one into database.
}