CodeIgniter 3 - 多文件上传

时间:2015-06-09 13:13:56

标签: php codeigniter file-upload

我是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

3 个答案:

答案 0 :(得分: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;
}

答案 1 :(得分:0)

$ this-&gt; upload-&gt; 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

答案 2 :(得分:0)

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

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