0

我能够在数据库中上传和插入图像。但是我无法上传多张图片,我应该在下面的代码中更改什么,以便上传多张图片

我的form.php

<?php echo form_open_multipart('admin/addproduct'); ?> 
        <table border="0" cellspacing="0" cellpadding="0" style="width:60%;"> 
            <tr>
                <td style="vertical-align: top;width:50%;text-align: right;"> Product name </td>
                <td style="vertical-align: top;width:50%;"><?php echo form_input($fpname) ?>  </td>
            </tr>  
            <tr>
                <td style="vertical-align: top;width:50%;text-align: right;"> Price </td>
                <td style="vertical-align: top;width:50%;"><?php echo form_input($fprice) ?>  </td>
            </tr> 
            <tr>
                <td style="vertical-align: top;width:50%;text-align: right;"> Image 1: </td>
                <td style="vertical-align: top;width:50%;"><?php echo form_upload("userfile"); ?></td>
            </tr>
             <tr>
                <td style="vertical-align: top;width:50%;text-align: right;"> Image 2: </td>
                <td style="vertical-align: top;width:50%;"><?php echo form_upload("img2"); ?>    </td>
            </tr>
             <tr>
                <td style="vertical-align: top;width:50%;text-align: right;">Image 3:  </td>
                <td style="vertical-align: top;width:50%;"><?php echo form_upload("img3"); ?>    </td>
            </tr>
             <tr>
                <td style="vertical-align: top;width:50%;text-align: right;"> Details </td>
                <td style="vertical-align: top;width:50%;"><?php echo form_textarea($fdetails) ?>  </td>
            </tr> 
            <tr>
                <td style="vertical-align: top;width:50%;text-align: center" colspan="2">
                    <input type="submit" value="Submit"/>
                </td> 
            </tr> 
        </table>  
      <?php echo form_close(); ?>  

控制器.php

      function addproduct() {
        $config['upload_path'] = './uploads/';
        $config['allowed_types'] = 'gif|jpg|png';
        $config['max_size'] = '1879800';
        $config['max_width'] = '1024';
        $config['max_height'] = '2768';
  $config['encrypt_name'] = TRUE;
        $this->load->library('upload', $config);
        $this->load->library('form_validation');
        $this->form_validation->set_error_delimiters('<li>', '</li>')->set_rules('pname', 'Product Name', 'trim|min_length[4]|required|max_length[112]');
        $this->form_validation->set_error_delimiters('<li>', '</li>')->set_rules('details', 'Details', 'trim|min_length[4]|required|max_length[12]');
        $this->form_validation->set_error_delimiters('<li>', '</li>')->set_rules('price', 'Price', 'trim|min_length[4]|required|max_length[112]|numeric');

        if (!$this->form_validation->run()) {//--false
            $data = $this->productmodel->productdetails();
            $this->load->view('admin/home', $data);
        }

        if (!$this->upload->do_upload()) {  // no file uploaded or failed upload
            $error = array('error' => $this->upload->display_errors());
            $data = $this->productmodel->productdetails();
            print_r($error);
            $this->load->view('admin/home', $data);
        } 
        else 
        {  
            $data =$this->upload->data();
            $this->productmodel->entry_insert_productdetails($data["file_name"]); 
            $data = $this->productmodel->productdetails();
              $this->session->set_flashdata('msg', 'One record added successfully...'); 
              redirect(base_url().'index.php/admin/index');
        }
    }

更新:从表单生成的源代码

 <form action="http://localhost/wclick/index.php/admin/addproduct" method="post" accept-charset="utf-8" enctype="multipart/form-data"> 
        <table border="0" cellspacing="0" cellpadding="0" style="width:60%;"> 
            <tr>
                <td style="vertical-align: top;width:50%;text-align: right;"> Product name </td>
                <td style="vertical-align: top;width:50%;"><input type="text" name="pname" value="" maxlength="190" size="20"  />  </td>
            </tr>  
            <tr>
                <td style="vertical-align: top;width:50%;text-align: right;"> Price </td>
                <td style="vertical-align: top;width:50%;"><input type="text" name="price" value="" maxlength="20" size="20" placeholder=" " id="txtsubject"  />  </td>
            </tr> 
            <tr>
                <td style="vertical-align: top;width:50%;text-align: right;"> Image 1: </td>
                <td style="vertical-align: top;width:50%;"><input type="file" name="userfile" value=""  /></td>
            </tr>
             <tr>
                <td style="vertical-align: top;width:50%;text-align: right;"> Image 2: </td>
                <td style="vertical-align: top;width:50%;"><input type="file" name="img2" value=""  />    </td>
            </tr>
             <tr>
                <td style="vertical-align: top;width:50%;text-align: right;">Image 3:  </td>
                <td style="vertical-align: top;width:50%;"><input type="file" name="img3" value=""  />    </td>
            </tr>
             <tr>
                <td style="vertical-align: top;width:50%;text-align: right;"> Details </td>
                <td style="vertical-align: top;width:50%;"><textarea name="details" cols="20" rows="4" ></textarea>  </td>
            </tr> 
            <tr>
                <td style="vertical-align: top;width:50%;text-align: center" colspan="2">
                    <input type="submit" value="Submit"/>
                </td> 
            </tr> 
        </table>  
      </form>
4

3 回答 3

2

每次在 do_upload 之前调用下面的函数

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

假设这个数组中的所有文件

$_FILES['userfile']['name']
那么你的脚本看起来像

        $this->load->library('upload');
    for ($k = 0; $k < count($_FILES['userfile']['name']); $k++) {
        $this->upload->initialize($upload); //must reinitialize to get rid of your bug ( i had it as well)
        if (!$this->upload->do_upload('userfile',$k)) {
            $this->load->view('upload/image_form', $data + array('error'=>$this->upload->display_errors()));
        }
        $udata[$k] = $this->upload->data(); //gradually build up upload->data()
    }
于 2013-10-11T12:30:03.410 回答
0

如果您查看 CI 手册,则必须提供上传的字段名称。

$field_name = "some_field_name";
$this->upload->do_upload($field_name);

您将需要对要更新的每个不同文件执行此操作,可能是在一个循环中。

于 2013-10-11T12:18:47.693 回答
0

请查看以下链接,因为它们可能会对您有所帮助:

使用 CodeIgniter 2.0 上传多个文件(数组)

http://ellislab.com/forums/viewthread/234635/#1051511

于 2013-10-11T12:19:52.430 回答