Codeigniter &HTML5 - 尝试一次上传多张图片 [英] Codeigniter & HTML5 - Trying to Upload Multiple Images at Once

查看:18
本文介绍了Codeigniter &HTML5 - 尝试一次上传多张图片的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

视图看起来像这样

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

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

i want the user to be able to upload multiple images at once. once uploaded i want to make an entry of the image details in the database, and finally move the image to the uploads folder.

我有codeigniter的基础知识

i have basic knowledge of codeigniter

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

ps: i dont want to use uploadify or similar image uploading plugins. trying to keep it as light weight as possible

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

Update this is the kind of array i am getting when i try var_dump($_FILES['userfile']). what kind of loop should i use to separate the data of the respective images.

 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	mpphp2AC2.tmp' (length=23)
      1 => string 'C:wamp	mpphp2AD3.tmp' (length=23)
  'error' => 
    array
      0 => int 0
      1 => int 0
  'size' => 
    array
      0 => int 409424
      1 => int 260343

推荐答案

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

I ran into this problem aswell. The $_FILES data is sent a diffent structure (due to the multiple="" attribute) so codeigniter can't handle it. Prepend this before the uploading process:

$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]);

然后在循环函数中使用这个:

Then in the loop function use this:

$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"/> 上发送的通用结构,然后运行一个循环,获取他们所有的键名.

Explanation:
I simply change the structure of $_FILES to the common structure which is sent on a default <input type="file" /> and the run a loop, fetching all their key names.

这篇关于Codeigniter &amp;HTML5 - 尝试一次上传多张图片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆