欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  后端开发

Codeigniter多文件上传与缩略图实现代码

程序员文章站 2022-04-19 15:28:19
...
  1. class Upload extends Controller {

  2. function go() {
  3. if(isset($_POST['go'])) {
  4. //初始化
  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图片

  14. $configThumb = array();
  15. $configThumb['image_library'] = 'gd2';
  16. $configThumb['source_image'] = '';
  17. $configThumb['create_thumb'] = TRUE;
  18. $configThumb['maintain_ratio'] = TRUE; //保持图片比例
  19. $configThumb['new_image'] = 'album/thumb';
  20. $configThumb['width'] = 170;
  21. $configThumb['height'] = 170;
  22. //600*600图片
  23. $configLarge = array();
  24. $configLarge['image_library'] = 'gd2';
  25. $configLarge['source_image'] = '';
  26. $configLarge['create_thumb'] = TRUE;
  27. $configLarge['maintain_ratio'] = TRUE; //保持图片比例
  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 $upload = $this->upload->do_upload('image'.$i);

  33. if($upload === FALSE) continue;
  34. $data = $this->upload->data();//返回上传文件的所有相关信息的数组
  35. $uid = $this->session->userdata('uid');
  36. $uploadedFiles[$i] = $data;
  37. if($data['is_image'] == 1) {

  38. //初始化170*170
  39. $configThumb['source_image'] = $data['full_path']; //文件路径带文件名
  40. $this->image_lib->initialize($configThumb);
  41. $this->image_lib->resize();
  42. //初始化600*600
  43. $configLarge['source_image'] = $data['full_path']; //文件路径带文件名
  44. $this->image_lib->initialize($configLarge);
  45. $this->image_lib->resize();
  46. }
  47. //插入图片信息到album表,插入的文件名为source目录文件名

  48. $picture = array(
  49. 'filename' => $data['file_name'],
  50. 'albumID' => $this->uri->segment(4,0),
  51. 'uid' => $this->session->userdata('uid'),
  52. 'dateline' => time(),
  53. 'describe' => '',
  54. 'click' => 0
  55. );
  56. $this->load->model('album_model');

  57. $this->album_model->AddPic($picture);
  58. $picture = array();
  59. }
  60. }
  61. /* 转出 */
  62. $albumID = $this->uri->segment(4);
  63. $backurl = site_url() . 'photo/editpic/album/' .$albumID;
  64. $this->session->set_flashdata('msg','图片上传成功.');
  65. redirect($backurl,'refresh');
  66. }
  67. }
复制代码

2,views:new_pic.view文件:







复制代码

type="submit" name="go" value="上传照片" class="button" />

注意事项: 1,要一次上传几个文件,修改表单和控制器中循环部分的参数就好。 2,album\\source 是上传后原图目录 large和thumb分别是两次执行$this->image_lib->resize();后存放缩略图的目录。 3,缩略图文件名,如需和album\\source目录一致,请添加参数 $config['thumb_marker'] = '';。 4,$picture 这部分数组是保存到数据库的东西,无需理会。