使用codeigniter进行多个图像上传

时间:2014-11-17 12:20:41

标签: php codeigniter file-upload

我正尝试使用以下代码进行多个图像上传和不同的文件输入

<input name="attachement[]" type="file">
<input name="attachement[]" type="file">

我的codeigniter代码是

$config = array(
                'upload_path' => $path,
                'max_size' => 1024 * 100,
                'allowed_types' => 'gif|jpeg|jpg|png',
                'overwrite' => true,
                'remove_spaces' => true);
            $images = array();
            $this->load->library('upload');

            $files = $_FILES;
            $count = count($_FILES[$attachName]['name']);


            for ($i = 0; $i < $count; $i++) {

                $_FILES[$attachName]['name'] = $files[$attachName]['name'][$i];
                $_FILES[$attachName]['type'] = $files[$attachName]['type'][$i];
                $_FILES[$attachName]['tmp_name'] = $files[$attachName]['tmp_name'][$i];
                $_FILES[$attachName]['error'] = $files[$attachName]['error'][$i];
                $_FILES[$attachName]['size'] = $files[$attachName]['size'][$i];

                $fileName = $title . '_' . $_FILES[$attachName]['name'];
                $images[] = $fileName;
                $config['file_name'] = $fileName;

                $this->upload->initialize($config);
                $this->upload->do_upload();
                if ($this->upload->do_upload()) {
                    $return['data'][] = $this->upload->data();
                    $return['status'] = "success";
                    $return['msg'] = sprintf($this->lang->line('success_item_added'), "Image", "uploaded");
                } else {
                    $return['status'] = 'danger';
                    $return['msg'] .= $this->upload->display_errors('', '') . "\r";
                }

            }

在上面的代码中,$ attachName =“attachement”

但我收到错误You did not select any file to upload

我做错了什么?

3 个答案:

答案 0 :(得分:3)

我找到了解决问题的方法

$this->upload->do_upload(); <-- removed this line

我改变了这一行

if ($this->upload->do_upload()) {

到这个

if ($this->upload->do_upload($attachName)) {

我希望这有助于某人

感谢那些没有发表评论的人,甚至是为什么

答案 1 :(得分:0)

对于多个文件选择和上传,您必须使用

 <input name="attachement[]" type="file" multiple>

否则总是选择一个文件,多个文件fileds会相互覆盖。

答案 2 :(得分:0)

请在enctype="multipart/form-data标记中加入<form>这个:

 <form method="post" enctype="multipart/form-data">
       <input name="attachement[]" type="file">
       <input name="attachement[]" type="file">
    </form>