上传与笨多张图片上传与笨多张图片(Upload multiple images with codei

2019-05-12 12:27发布

我已经使用一些代码,我发现前一次上传1个图像,这完美的作品。 我想使用的代码,但是对多个图像相同的风格。

该模型:

 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;
}

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

<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));
        }

是否有使用这个代码,所以我可以上传多张图片的方式?

Answer 1:

拥有此代码来看看这可能有助于了解你如何处理多张图片

       #####################
        # 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上传至目前的脚本项的关联数组method.for进一步看这个LINK

这是脚本的所有范围内的自动变量avaliable

 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标记不要不要忘记:

  1. 输入名称必须被定义为一个阵列,即名称=“文件[]”
  2. 输入元素必须有多个=“多”或只是多

    3. $这个 - >负载>库( '上传'); //加载库

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

    即指否极泰来回调$这 - > upload->数据()返回有关像的文件名,路径,大小等上传的文件数据的数组



文章来源: Upload multiple images with codeigniter