在Codeigniter中上传多个文件时出错

发布于 2025-01-05 22:02:38 字数 973 浏览 0 评论 0原文

我尝试在 CodeIgniter 中使用多个文件上传,但出现错误: 您没有选择要上传的文件

我的 html 代码:

<input type="file" id="ModelImage" name="ModelImage[]" multiple="multiple"  value=""/>

我的 php 代码:

for($i = 0; $i < count($_FILES['ModelImage']["name"]); $i++)
{
  if (!empty($_FILES['ModelImage']['name'][$i]))
  {
    $config['upload_path'] = './uploads/starmodel/';
    $config['allowed_types'] = 'gif|jpg|png';
    $config['file_name']= "star_".$lastid."_".$i.strrchr(basename($_FILES["ModelImage"]["name"][$i]),".");
    $this->upload->initialize($config);

    if ($this->upload->do_upload('ModelImage'))
    {
      $data = $this->upload->data();
      $udata = array('ModelImage' => $config['file_name']);
      $this->db->where('ModelID', $lastid);
      $this->db->update('starmodel', $udata);
    }
    else
    {
      echo $this->upload->display_errors();
    }
  }
}

I am trying to use multiple file upload in CodeIgniter, but I'm getting an error:
You did not select a file to upload.

My html code:

<input type="file" id="ModelImage" name="ModelImage[]" multiple="multiple"  value=""/>

My php code:

for($i = 0; $i < count($_FILES['ModelImage']["name"]); $i++)
{
  if (!empty($_FILES['ModelImage']['name'][$i]))
  {
    $config['upload_path'] = './uploads/starmodel/';
    $config['allowed_types'] = 'gif|jpg|png';
    $config['file_name']= "star_".$lastid."_".$i.strrchr(basename($_FILES["ModelImage"]["name"][$i]),".");
    $this->upload->initialize($config);

    if ($this->upload->do_upload('ModelImage'))
    {
      $data = $this->upload->data();
      $udata = array('ModelImage' => $config['file_name']);
      $this->db->where('ModelID', $lastid);
      $this->db->update('starmodel', $udata);
    }
    else
    {
      echo $this->upload->display_errors();
    }
  }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

千と千尋 2025-01-12 22:02:38

name 参数必须是userfile

根据CI 文件上传文档

默认情况下,上传例程期望文件来自表单
字段名为 userfile,并且表单必须是“multipart”类型:

<form method="post" action="some_action" enctype="multipart/form-data" />
    <input type="file" name="userfile" size="20" />
    <input type="submit" value="upload" />
</form>

话虽如此,我不相信 CI 的库当前支持 HTML5 的多个文件上传。您应该在 CI 论坛上讨论以获取自定义库。

The name parameter must be userfile

According to the CI File Uploading Documentation:

By default the upload routine expects the file to come from a form
field called userfile, and the form must be a "multipart" type:

<form method="post" action="some_action" enctype="multipart/form-data" />
    <input type="file" name="userfile" size="20" />
    <input type="submit" value="upload" />
</form>

With that said, I do not believe CI's library currently supports HTML5's multiple file uploads. You should peak around CI's forums for custom libraries.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文