Codeigniter enables multiple file uploads and creates multiple thumbnails

  • 2021-06-29 10:44:08
  • OfStack

The program can be implemented:
1. Upload 5 pictures at the same time
2. Generate thumbnails of both sizes at the same time
3. Save to mysql

controllers:upload.php file:

<?php
class Upload extends Controller {
  function go() {
    if(isset($_POST['go'])) {
      // Initialization 
      $config['upload_path'] = 'album/source'; 
      $config['allowed_types'] = 'gif|jpg|png|bmp|jpeg';
      $config['encrypt_name'] = TRUE;
      $config['remove_spaces'] = TRUE;
      $config['max_size']  = '0';
      $config['max_width']  = '0';
      $config['max_height']  = '0';

      $this->load->library('upload', $config);

      //170*170 picture 
      $configThumb = array();
      $configThumb['image_library'] = 'gd2';
      $configThumb['source_image'] = '';
      $configThumb['create_thumb'] = TRUE;
      $configThumb['maintain_ratio'] = TRUE; // Keep Picture Scale 
      $configThumb['new_image'] = 'album/thumb';
      $configThumb['width'] = 170;
      $configThumb['height'] = 170;
      //600*600 picture 
      $configLarge = array();
      $configLarge['image_library'] = 'gd2';
      $configLarge['source_image'] = '';
      $configLarge['create_thumb'] = TRUE;
      $configLarge['maintain_ratio'] = TRUE; // Keep Picture Scale 
      $configLarge['new_image'] = 'album/large';
      $configLarge['width'] = 600;
      $configLarge['height'] = 600;

      $this->load->library('image_lib');

      for($i = 1; $i < 6; $i++) {
        $upload = $this->upload->do_upload('image'.$i);       
        if($upload === FALSE) continue;
        $data = $this->upload->data();// Returns an array of all relevant information about the uploaded file 
        $uid = $this->session->userdata('uid');
        $uploadedFiles[$i] = $data;

        if($data['is_image'] == 1) {
          // Initialization 170*170  
          $configThumb['source_image'] = $data['full_path']; // File path with file name 
          $this->image_lib->initialize($configThumb);
          $this->image_lib->resize();
          // Initialization 600*600
          $configLarge['source_image'] = $data['full_path']; // File path with file name 
          $this->image_lib->initialize($configLarge);
          $this->image_lib->resize();
        }

        // Insert picture information into album surface , Inserted file name source Directory File Name 
        $picture = array(
            'filename' => $data['file_name'],
            'albumID' => $this->uri->segment(4,0),
            'uid' => $this->session->userdata('uid'),
            'dateline' => time(),
            'describe' => '',
            'click' => 0
        );

        $this->load->model('album_model');
        $this->album_model->AddPic($picture);
        $picture = array();
      }
    }
    /*  Outgoing  */
    $albumID = $this->uri->segment(4);
    $backurl = site_url() . 'photo/editpic/album/' .$albumID;
    $this->session->set_flashdata('msg',' Picture uploaded successfully .');
    redirect($backurl,'refresh');
  }
}

views:new_pic.view file:

<form method="post" action="<?php echo site_url() ?>photo/upload/go/<?php echo $albumID ?>" enctype="multipart/form-data">
  <input type="file" name="image1" class="files"/><br />
  <input type="file" name="image2" class="files"/><br />
  <input type="file" name="image3" class="files"/><br />
  <input type="file" name="image4" class="files"/><br />
  <input type="file" name="image5" class="files"/><br />
  <br />
  <p><input type="submit" name="go" value=" Upload photos " class="button" /></p>
</form>

Also note:

1. To upload several files at a time, modify the parameters of the loop section in the form and controller.
2. album\source is the original map directory after uploading large and thumb are executed twice $this- > image_lib- > resize();Directory to store thumbnails after
3. For a thumbnail file name that matches albumsource directory 1, add the parameter $config['thumb_marker'] ='';
4. $picture This part of the group is saved to the database, you can leave it alone.


Related articles: