【问题标题】:I cant upload Multiple file upload in codeigniter我无法在codeigniter中上传多个文件上传
【发布时间】:2012-09-18 21:23:22
【问题描述】:

这是我的视图(html)内容

<tr>
    <td width="341" class="linkssubhead">
        Subcategory Image&nbsp;<span class="errormsg">*</span>
    </td>
    <td width="965" align="left">
        <input type="file" name="subcategory_image" value=""  class="required" />&nbsp;&nbsp;<span class="errormsg"><?php  echo $file_error ; ?> </span>
    </td>       
</tr>
<tr>
    <td width="341" class="linkssubhead">
        Subcategory Banner Image&nbsp;<span class="errormsg">*</span>
    </td>
    <td width="965" align="left">
        <input type="file" name="subcategory_banner_image" value=""  class="required" />&nbsp;&nbsp;<span class="errormsg"><?php  echo $file_error ; ?> </span>
    </td>       
</tr>
<tr>
    <td width="341" class="linkssubhead">
        Subcategory Banner Logo&nbsp;<span class="errormsg">*</span>
    </td>
    <td width="965" align="left">
        <input type="file" name="subcategory_banner_logo" value=""  class="required" />&nbsp;&nbsp;<span class="errormsg"><?php  echo $file_error ; ?> </span>
    </td>       
</tr>

这是我的控制器

$config_image['allowed_types'] =  'gif|jpg|png';
$config_image['overwrite'] =TRUE;
$config_image['subcategory_image']['upload_path'] = './public/subcategory/';
$config_image['subcategory_banner_image']['upload_path'] = './public/subcategory/bannerimage/';
$config_image['subcategory_banner_logo']['upload_path'] = './public/subcategory/bannerlogo/';
$config_image['subcategory_image']['file_name'] = $this->input->post('subcategory_name');
$config_image['subcategory_banner_image']['file_name'] = 'banner_image_'.$this->input->post('subcategory_name');
$config_image['subcategory_banner_logo']['file_name'] =  'banner_logo_'.$this->input->post('subcategory_name');

$this->load->library('upload', $config_image);
$this->upload->initialize($config_image); 
if (!$this->upload->do_upload(array('subcategory_image','subcategory_banner_image','subcategory_banner_logo')))
{
    $errors = $this->upload->display_errors();
    if ($errors)
    {
        $data['file_error'] = $errors;
        $data['body'] =  $this->load->view('matress_model/add_subcategory',$data,TRUE);
        $this->load->view('page',$data);
    } 
}
else
{
    redirect('fileupload/sucess');
}

提交表单后出现错误

Severity: Warning
Message: Illegal offset type in isset or empty
Filename: libraries/Upload.php
Line Number: 140

如何摆脱这种情况以及如何允许上传多个文件?

【问题讨论】:

  • 我不确定您是否可以将数组传递给do_upload() 方法——您应该尝试循环遍历您的文件并一次执行一个。参考:Codeigniter File Uploading Class
  • 另外,如果你已经将配置数组传递给库加载,你也不需要初始化配置。这仅在您更改了配置时才有用,例如第二次循环...提示,提示。
  • 你能给我看看代码吗...
  • 不,我不会为你编写代码。我给了你一个文档链接,以及一个关于如何处理这个问题的严肃提示。先试试看;如果你卡住了,再问一个问题是什么失败了。

标签: codeigniter file-upload codeigniter-2


【解决方案1】:

为每张图片设置一个单独的配置数组,然后上传。如下所示

//I have given an exmaple for one image repeat this for others,
$config_image[0]['allowed_types'] =  'gif|jpg|png';
$config_image[0]['overwrite'] =TRUE;
$config_image[0]['subcategory_image']['upload_path'] = './public/subcategory/';
$config_image[0]['subcategory_image']['file_name'] = $this->input->post('subcategory_name');


//Load and initialize the library as below and then upload
$this->load->library('upload', $config_image[0]);
$this->upload->initialize($config_image[0]); 

【讨论】:

  • 既然我们可以返回一个数据数组,所有的信息文件都上传了吗?
猜你喜欢
  • 2017-01-26
  • 2012-01-12
  • 2013-12-05
  • 1970-01-01
  • 2020-02-05
  • 1970-01-01
  • 2019-07-23
  • 2016-01-30
相关资源
最近更新 更多