CodeIgniter implement multiple file uploads and create multiple thumbnail _php instances

Source: Internet
Author: User

The program can implement:
1. Upload 5 photos at the same time
2. Generate thumbnails of two sizes at the same time
3. Save to MySQL

controllers:upload.php file:

Copy Code code as follows:
<?php
Class Upload extends Controller {
function Go () {
if (isset ($_post[' Go ')) {
Class
$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 to album table, insert file name as source directory filename
$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 upload succeeded. ')
Redirect ($backurl, ' refresh ');
}
}

Views:new_pic.view file:

Copy Code code as follows:
<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>

In addition, you need to note:

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

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.