CodeIgniter implement multiple file uploads and create multiple thumbnail _php instances

Source: Internet
Author: User
The program can implement:
1. Upload 5 photos simultaneously
2. Simultaneous generation of two-size thumbnail images
3. Save to MySQL

controllers:upload.php file:

Copy CodeThe code is as follows: 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 Pictures
$configThumb = Array ();
$configThumb [' image_library '] = ' gd2 ';
$configThumb [' source_image '] = ';
$configThumb [' create_thumb '] = TRUE;
$configThumb [' maintain_ratio '] = TRUE; Maintain picture proportions
$configThumb [' new_image '] = ' album/thumb ';
$configThumb [' width '] = 170;
$configThumb [' height '] = 170;
600*600 Pictures
$configLarge = Array ();
$configLarge [' image_library '] = ' gd2 ';
$configLarge [' source_image '] = ';
$configLarge [' create_thumb '] = TRUE;
$configLarge [' maintain_ratio '] = TRUE; Maintain picture proportions
$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) {
Initialize 170*170
$configThumb [' source_image '] = $data [' Full_path ']; File path with file name
$this->image_lib->initialize ($CONFIGTHUMB);
$this->image_lib->resize ();
Initialize 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 the album table, insert the file name as the 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 ();
}
}

/* Turn out */
$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:

Copy the Code code as follows:

Also be aware that:

1. To upload several files at once, modify the parameters of the loop part of the form and controller as well.
2.album\\source is uploaded after the original directory large and thumb are two times executed $this->image_lib->resize ();
3. thumbnail file name should be consistent with Album\\source directory, please add parameter $config [' thumb_marker '] = ';
4. $picture This part of the array is something that is saved to the database and can be used without the control.

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.