Code for multifile Upload and thumbnails of Codeigniter
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 ');
- }
- }
2, views: new_pic.view file:
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 directory where the original image directory large and thumb run $ this-> image_lib-> resize () twice. 3. the name of the thumbnail file. to make it consistent with the album \ source directory, add the parameter $ config ['thumb _ marker'] = '';. 4, $ picture this part of the array is saved to the database, ignore it.
|