代码点火器&;HTML5-尝试一次上传多个图像


Codeigniter & HTML5 - Trying to Upload Multiple Images at Once

视图看起来像这样

<?=form_open_multipart('upload/process');?>
        <input type="file" multiple="multiple" name="userfile[]" id="userfile" />
        <?=form_submit('upload', 'Upload');?>
    <?=form_close();?>

我希望用户能够一次上传多个图像。上传后,我想在数据库中输入图像的详细信息,最后将图像移到上传文件夹中。

我有代码点火器的基本知识

ps:我不想使用uploadify或类似的图片上传插件。尽量保持的重量尽可能轻

更新这是我尝试var_dump($_FILES['userfile'])时得到的数组类型。我应该使用什么样的循环来分离各个图像的数据。

 array
  'name' => 
    array
      0 => string '01.jpg' (length=6)
      1 => string '1 (26).jpg' (length=10)
  'type' => 
    array
      0 => string 'image/jpeg' (length=10)
      1 => string 'image/jpeg' (length=10)
  'tmp_name' => 
    array
      0 => string 'C:'wamp'tmp'php2AC2.tmp' (length=23)
      1 => string 'C:'wamp'tmp'php2AD3.tmp' (length=23)
  'error' => 
    array
      0 => int 0
      1 => int 0
  'size' => 
    array
      0 => int 409424
      1 => int 260343

我也遇到了这个问题。$_FILES数据发送了一个不同的结构(由于multiple=""属性),因此codeigniter无法处理它。在上传过程之前准备:

$arr_files  =   @$_FILES['userfile'];
$_FILES     =   array();
foreach(array_keys($arr_files['name']) as $h)
$_FILES["file_{$h}"]    =   array(  'name'      =>  $arr_files['name'][$h],
                                    'type'      =>  $arr_files['type'][$h],
                                    'tmp_name'  =>  $arr_files['tmp_name'][$h],
                                    'error'     =>  $arr_files['error'][$h],
                                    'size'      =>  $arr_files['size'][$h]);

然后在循环函数中使用以下内容:

$this->load->library('upload');
$arr_config =   array(  'allowed_types' =>  'gif|jpg|png',
                            'upload_path'   =>  'url_path/');
foreach(array_keys($_FILES) as $h) {
    // Initiate config on upload library etc.
    $this->upload->initialize($arr_config);
    if ($this->upload->do_upload($h)) {
        $arr_file_data  =   $this->upload->data();
    }
}

解释:
我只需将$_FILES的结构更改为在默认<input type="file" />上发送的公共结构,然后运行一个循环,获取它们的所有密钥名称。

您需要的循环:

for($i=0; $i<count($_FILES['name']); $i++){
    if ($_FILES['error'][$i] == 0) {
        //do your stuff here, each image is at $_FILES['tmp_name'][$i]
    }
}

注意,is没有使用CI上传类,而是使用普通的PHP,我通常觉得它比CI的类更容易使用。

检查此类以进行多文件上传https://github.com/stvnthomas/CodeIgniter-Multi-Upload

这对我很有用。