使用codeigniter上传多个图像 [英] Upload multiple images with codeigniter

查看:130
本文介绍了使用codeigniter上传多个图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用了一些我之前发现的代码,一次上传1张图片,这完美。

I have used some code I found before to upload 1 image at a time and this works perfect. I would like to use the same style of code but for multiple images.

模型:

 function cover_upload($afbeelding) {
    // path where the picture needs to be uploaded
    echo $album_path = APPPATH . 'images/covers';
    // configuration for the upload
    $ext = end(explode(".", $_FILES['userfile']['name']));
    $config = array(
        'file_name' => $afbeelding . '.' . $ext,
        'upload_path' => $album_path,
        'allowed_types' => 'gif|jpg|jpeg|png',
        'max_size' => '5120'
    );
    // load upload library with the configuration
    $this->load->library('upload', $config);
    // upload picture with the upload library
    if (!$this->upload->do_upload()) {
        echo $this->upload->display_errors();
        die();
    }
    // get data array of the uploaded picture
    $image_data = $this->upload->data();
    // configuration for the picture thumbnail resize
    echo $image_data['full_path'];
    $config = array(
        'source_image' => $image_data['full_path'],
        'new_image' => realpath($album_path . '/thumb'),
        'maintain_ration' => TRUE,
        'width' => 300,
        'height' => 300
    );
    // load image manupulation library with the configuration
    $this->load->library('image_lib', $config);
    // resize picture
    $this->image_lib->resize();
    $this->image_lib->clear();
    // submit file name of the uploaded picture to save it in the database
    $bestandsnaam = $image_data['file_name'];
    return $bestandsnaam;
}

视图(只是关于图片的部分):

the view (just the part about the image):

<div class="form-group">
            <label class="col-sm-2 control-label" for="CoverFoto">picture</label>
            <div class="col-sm-5"> 
                <div class="fileinput fileinput-new" data-provides="fileinput">
                    <div class="fileinput-preview thumbnail" data-trigger="fileinput" style="width: 200px; height: 150px;"></div>
                    <div>
                        <span class="btn btn-default btn-file"><span class="fileinput-new">select_an_image</span><span class="fileinput-exists">change</span><input type="file" name="userfile"></span>
                        <a href="#" class="btn btn-default fileinput-exists" data-dismiss="fileinput"><?php echo $this->lang->line("remove"); ?></a>
                    </div>
                </div>
            </div>
        </div>

和控制器:

if (!empty($_FILES['userfile']['name'])) {
            $gamma->CoverFoto = $this->gamma_model->cover_upload(url_title($gamma->Naam, '_', true));
        }

有没有办法使用这个代码,所以我可以上传多张图片? / p>

Is there a way to use this code so i can upload multiple images?

推荐答案

查看此代码可能有助于了解如何处理多个图像

Have a look upon this code may this help in understanding you how to handle multiple images

       #####################
        # Uploading multiple# 
        #     Images        #
        #####################


        $files = $_FILES;
        $count = count($_FILES['uploadfile']['name']);
        for($i=0; $i<$count; $i++)
                {
                $_FILES['uploadfile']['name']= $files['uploadfile']['name'][$i];
                $_FILES['uploadfile']['type']= $files['uploadfile']['type'][$i];
                $_FILES['uploadfile']['tmp_name']= $files['uploadfile']['tmp_name'][$i];
                $_FILES['uploadfile']['error']= $files['uploadfile']['error'][$i];
                $_FILES['uploadfile']['size']= $files['uploadfile']['size'][$i];
                $this->upload->initialize($this->set_upload_options());//function defination below
                $this->upload->do_upload('uploadfile');
                $upload_data = $this->upload->data();
                $name_array[] = $upload_data['file_name'];
                $fileName = $upload_data['file_name'];
                $images[] = $fileName;

                }
              $fileName = $images;

代码中发生了什么

$ _ FILE ---->它是通过POST方法上传到当前脚本的项目的关联数组。进一步查看 LINK

well $_FILE---->it is an associative array of items uploaded to the current script via the POST method.for further look this LINK

它是在脚本的所有范围内可用的自动变量

it's an automatic variable avaliable within all scopes of script

 function set_upload_options()
  { 
  // upload an image options
         $config = array();
         $config['upload_path'] = LARGEPATH; //give the path to upload the image in folder
         $config['remove_spaces']=TRUE;
         $config['encrypt_name'] = TRUE; // for encrypting the name
         $config['allowed_types'] = 'gif|jpg|png';
         $config['max_size'] = '78000';
         $config['overwrite'] = FALSE;
         return $config;
  }

并且在您的HTML标记中不要忘记:

and in your html markup don't don't forget:


  1. 输入名称必须定义为数组,即name =file []

  2. 输入元素必须有multiple =multiple或只有多个。

  1. Input name must be be defined as an array i.e. name="file[]"
  2. Input element must have multiple="multiple" or just multiple

3. $ this-> load-> library('upload'); //加载库

3.$this->load->library('upload'); //to load library

4.回调,$ this-> upload-> do_upload()会将在给定字段名中选择的文件上传到目标文件夹。

4.The callback, $this->upload->do_upload() will upload the file selected in the given field name to the destination folder.

5.回调$ this-> upload-> data()返回与上传文件相关的数据数组,如文件名,路径,大小等。

5.And the callback $this->upload->data() returns an array of data related to the uploaded file like the file name, path, size etc.

这篇关于使用codeigniter上传多个图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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