【问题标题】:CodeIgniter 3 - Multi files uploadCodeIgniter 3 - 多文件上传
【发布时间】:2023-03-22 18:05:02
【问题描述】:

我是 CI 3 的初学者,我想上传两个文件图像。我尝试了谷歌,但没有任何效果。 感谢您的所有帮助

HTML 视图

<form method="post" accept-charset="utf-8" action="Kasprof" enctype="multipart/form-data">
      <div class="form-group">
            Parent / Potvrdenie zákonného zástupcu
            <input name="images[parent]" type="file">
      </div>
      <div class="form-group">
            Doctor / Potvrdenie od doktora
            <input name="images[doctor]" type="file">
      </div>              
      <button type="submit" class="btn btn-default">Send / Poslať</button>
</form>

PHP 控制器:

$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'gif|jpg|png|pdf|jpeg';
$config['max_size'] = "4096000";
$config['max_width'] = "4096";
$config['max_height'] = "4096";
$this->upload->initialize($config);

$this->upload->do_upload($_FILES);

错误:

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

Message: preg_match_all() expects parameter 2 to be string, array given
Filename: libraries/Upload.php
Line Number: 382

【问题讨论】:

  • @Tool 我忘了对不起。

标签: php codeigniter file-upload


【解决方案1】:

您可以使用此功能上传多个上传。 其中$userfile 是输入文件名,$image_path 是您的目标路径,$allowed 允许的类型,$max_size 允许的最大上传大小。

function _multi_upload_files($userfile,$image_path,$allowed,$max_size)
{
    $this->ci->load->library('upload');
    if(!is_dir($image_path))
    {
        mkdir($image_path);
    }
    $files = $_FILES;
    $cpt = count($_FILES[$userfile]['name']);
    for($i=0; $i<$cpt; $i++)
    {
       if($files[$userfile]['tmp_name'][$i]!='')
       {
            $_FILES[$userfile]['name']= $files[$userfile]['name'][$i];
            $_FILES[$userfile]['type']= $files[$userfile]['type'][$i];
            $_FILES[$userfile]['tmp_name']= $files[$userfile]['tmp_name'][$i];
            $_FILES[$userfile]['error']= $files[$userfile]['error'][$i];
            $_FILES[$userfile]['size']= $files[$userfile]['size'][$i];    

            $config['upload_path'] = $image_path;
            $config['allowed_types'] = $allowed;
            $config['max_size'] = $max_size;
            // if want to rename file
            $img=$_FILES[$userfile]['name'][$i];
            $random_digit=rand(00,99999);
            $ext = strtolower(substr($img, strpos($img,'.'), strlen($img)-1));
            $file_name=$random_digit.$ext;
            $config['file_name'] = $file_name;
            // end renaming
            $this->ci->upload->initialize($config);
            $this->ci->upload->do_upload($userfile);
            $newfile[]=$this->ci->upload->file_name;
       }
    }

    return $newfile;
}

【讨论】:

    【解决方案2】:

    $this->upload->do_upload() 需要一个字段名而不是 $_FILES 数组。

    $this->upload->do_upload('images[parent]');
    /*/
     * error handeling
    /*/
    $this->upload->do_upload('images[doctor]');
    /*/
     * error handeling
    /*/
    

    这将上传 2 张图片

    https://ellislab.com/codeigniter/user-guide/libraries/file_uploading.html

    【讨论】:

      【解决方案3】:

      循环上传的文件应该可以工作。

      foreach($_FILES as $userfile){
         //some code here
      }
      

      【讨论】:

        猜你喜欢
        • 2015-04-28
        • 2012-03-05
        • 2013-07-31
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多