Code for multifile Upload and thumbnails of Codeigniter

Source: Internet
Author: User
Code for multifile Upload and thumbnails of Codeigniter

  1. Class Upload extends Controller {

  2. Function go (){
  3. If (isset ($ _ POST ['go']) {
  4. // Initialization
  5. $ Config ['upload _ path'] = 'Album/source ';
  6. $ Config ['allowed _ types'] = 'gif | jpg | png | bmp | jpeg ';
  7. $ Config ['encryption _ name'] = TRUE;
  8. $ Config ['remove _ spaces'] = TRUE;
  9. $ Config ['max _ size'] = '0 ';
  10. $ Config ['max _ width'] = '0 ';
  11. $ Config ['max _ height'] = '0 ';

  12. $ This-> load-> library ('upload', $ config );

  13. // 170*170 images

  14. $ ConfigThumb = array ();
  15. $ ConfigThumb ['image _ library'] = 'gd2 ';
  16. $ ConfigThumb ['Source _ image'] = '';
  17. $ ConfigThumb ['create _ thumb'] = TRUE;
  18. $ ConfigThumb ['maintain _ ratio '] = TRUE; // preserve the image proportion
  19. $ ConfigThumb ['new _ image'] = 'Album/thumb ';
  20. $ ConfigThumb ['width'] = 170;
  21. $ ConfigThumb ["height"] = 170;
  22. // 600*600 images
  23. $ ConfigLarge = array ();
  24. $ ConfigLarge ['image _ library'] = 'gd2 ';
  25. $ ConfigLarge ['Source _ image'] = '';
  26. $ ConfigLarge ['create _ thumb'] = TRUE;
  27. $ ConfigLarge ['maintain _ ratio '] = TRUE; // preserve the image proportion
  28. $ ConfigLarge ['new _ image'] = 'Album/large ';
  29. $ ConfigLarge ['width'] = 600;
  30. $ ConfigLarge ['height'] = 600;

  31. $ This-> load-> library ('image _ lib ');

  32. For ($ I = 1; $ I <6; $ I ++ ){

  33. $ Upload = $ this-> upload-> do_upload ('image'. $ I );
  34. If ($ upload = FALSE) continue;
  35. $ Data = $ this-> upload-> data (); // returns an array of all relevant information of the uploaded file.
  36. $ Uid = $ this-> session-> userdata ('uid ');
  37. $ UploadedFiles [$ I] = $ data;

  38. If ($ data ['is _ image'] = 1 ){

  39. // Initialize 170*170
  40. $ ConfigThumb ['Source _ image'] = $ data ['full _ path']; // file path with file name
  41. $ This-> image_lib-> initialize ($ configThumb );
  42. $ This-> image_lib-> resize ();
  43. // Initialize 600*600
  44. $ ConfigLarge ['Source _ image'] = $ data ['full _ path']; // file path with file name
  45. $ This-> image_lib-> initialize ($ configLarge );
  46. $ This-> image_lib-> resize ();
  47. }

  48. // Insert the image information to the album table. the inserted file name is the source directory file name.

  49. $ Picture = array (
  50. 'Filename' => $ data ['File _ name'],
  51. 'Albumid' => $ this-> uri-> segment ),
  52. 'Uid' => $ this-> session-> userdata ('uid '),
  53. 'Dateline '=> time (),
  54. 'Describe' => '',
  55. 'Click' => 0
  56. );

  57. $ This-> load-> model ('Album _ Model ');

  58. $ This-> album_model-> AddPic ($ picture );
  59. $ Picture = array ();
  60. }
  61. }
  62. /* Transfer */
  63. $ AlbumID = $ this-> uri-> segment (4 );
  64. $ Backurl = site_url (). 'photo/editpic/album/'. $ albumID;
  65. $ This-> session-> set_flashdata ('MSG ', 'image uploaded successfully .');
  66. Redirect ($ backurl, 'refresh ');
  67. }
  68. }

2, views: new_pic.view file:

  1. 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.

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.