This program can achieve:
1. Upload 5 images at the same time
2. generate two types of thumbnails at the same time
3. Save to mysql
Controllers: upload. php file:
Copy codeThe Code is as follows: <? 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 ['encryption _ 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 Images
$ ConfigThumb = array ();
$ ConfigThumb ['image _ library'] = 'gd2 ';
$ ConfigThumb ['source _ image'] = '';
$ ConfigThumb ['create _ thumb'] = TRUE;
$ ConfigThumb ['maintain _ ratio '] = TRUE; // preserve the image proportion
$ ConfigThumb ['new _ image'] = 'album/thumb ';
$ ConfigThumb ['width'] = 170;
$ ConfigThumb ["height"] = 170;
// 600*600 Images
$ ConfigLarge = array ();
$ ConfigLarge ['image _ library'] = 'gd2 ';
$ ConfigLarge ['source _ image'] = '';
$ ConfigLarge ['create _ thumb'] = TRUE;
$ ConfigLarge ['maintain _ ratio '] = TRUE; // preserve the image proportion
$ 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 of 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 the image information to the album table. The inserted file name is the source directory file name.
$ Picture = array (
'Filename' => $ data ['file _ name'],
'Albumid' => $ this-> uri-> segment ),
'Uid' => $ this-> session-> userdata ('uid '),
'Dateline '=> time (),
'Describe' => '',
'Click' => 0
);
$ This-> load-> model ('album _ model ');
$ This-> album_model-> AddPic ($ picture );
$ Picture = array ();
}
}
/* Transfer */
$ AlbumID = $ this-> uri-> segment (4 );
$ Backurl = site_url (). 'photo/editpic/album/'. $ albumID;
$ This-> session-> set_flashdata ('msg ', 'image uploaded successfully .');
Redirect ($ backurl, 'refresh ');
}
}
Views: new_pic.view file:
Copy codeThe Code is 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 photo" class = "button"/> </p>
</Form>
Note the following:
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 directory of the uploaded source image. large and thumb run $ this-> image_lib-> resize () twice.
3. If the thumbnail file name must 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, you don't need to worry about it.