CodeIgniter multi-File upload and thumbnail implementation code

Source: Internet
Author: User
Tags 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 [' encrypt_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 Pictures

  14. $configThumb = Array ();
  15. $configThumb [' image_library '] = ' gd2 ';
  16. $configThumb [' source_image '] = ';
  17. $configThumb [' create_thumb '] = TRUE;
  18. $configThumb [' maintain_ratio '] = TRUE; Maintain picture proportions
  19. $configThumb [' new_image '] = ' album/thumb ';
  20. $configThumb [' width '] = 170;
  21. $configThumb [' height '] = 170;
  22. 600*600 Pictures
  23. $configLarge = Array ();
  24. $configLarge [' image_library '] = ' gd2 ';
  25. $configLarge [' source_image '] = ';
  26. $configLarge [' create_thumb '] = TRUE;
  27. $configLarge [' maintain_ratio '] = TRUE; Maintain picture proportions
  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 about 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 picture information into the album table, insert the file name as the source directory file name

  49. $picture = Array (
  50. ' filename ' = $data [' file_name '],
  51. ' albumID ' = $this->uri->segment (4,0),
  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. /* Turn out */
  63. $albumID = $this->uri->segment (4);
  64. $backurl = Site_url (). ' photo/editpic/album/'. $albumID;
  65. $this->session->set_flashdata (' msg ', ' picture uploaded successfully. ');
  66. Redirect ($backurl, ' refresh ');
  67. }
  68. }

Copy Code

2,views:new_pic.view file:

    1. Note: 1, to upload a few files at once, modify the form and controller in the loop part of the parameters is good. 2,album\\source is uploaded after the original directory large and thumb are two times executed $this->image_lib->resize (), after the directory to store thumbnails. 3, thumbnail file name, if required and Album\\source directory consistent, please add parameters $config [' thumb_marker '] = ';. 4, $picture This part of the array is something to save to the database, regardless.

  • Related Article

    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.