用Codeigniter解决多文件上传并创建多个缩略图的代码示例
分类:PHP_Python| 发布:佚名| 查看:383 | 发表时间:2014/8/10
该程序可以实现:
1.同时上传5张图片
2.同时生成两种尺寸的缩略图
3.保存到mysql
controllers:upload.php文件:
代码如下:
02 | class Upload extends Controller { |
04 | if (isset( $_POST [ 'go' ])) { |
06 | $config [ 'upload_path' ] = 'album/source' ; |
07 | $config [ 'allowed_types' ] = 'gif|jpg|png|bmp|jpeg' ; |
08 | $config [ 'encrypt_name' ] = TRUE; |
09 | $config [ 'remove_spaces' ] = TRUE; |
10 | $config [ 'max_size' ] = '0' ; |
11 | $config [ 'max_width' ] = '0' ; |
12 | $config [ 'max_height' ] = '0' ; |
14 | $this ->load->library( 'upload' , $config ); |
17 | $configThumb = array (); |
18 | $configThumb [ 'image_library' ] = 'gd2' ; |
19 | $configThumb [ 'source_image' ] = '' ; |
20 | $configThumb [ 'create_thumb' ] = TRUE; |
21 | $configThumb [ 'maintain_ratio' ] = TRUE; |
22 | $configThumb [ 'new_image' ] = 'album/thumb' ; |
23 | $configThumb [ 'width' ] = 170; |
24 | $configThumb [ 'height' ] = 170; |
26 | $configLarge = array (); |
27 | $configLarge [ 'image_library' ] = 'gd2' ; |
28 | $configLarge [ 'source_image' ] = '' ; |
29 | $configLarge [ 'create_thumb' ] = TRUE; |
30 | $configLarge [ 'maintain_ratio' ] = TRUE; |
31 | $configLarge [ 'new_image' ] = 'album/large' ; |
32 | $configLarge [ 'width' ] = 600; |
33 | $configLarge [ 'height' ] = 600; |
35 | $this ->load->library( 'image_lib' ); |
37 | for ( $i = 1; $i < 6; $i ++) { |
38 | $upload = $this ->upload->do_upload( 'image' . $i ); |
39 | if ( $upload === FALSE) continue ; |
40 | $data = $this ->upload->data(); |
41 | $uid = $this ->session->userdata( 'uid' ); |
42 | $uploadedFiles [ $i ] = $data ; |
44 | if ( $data [ 'is_image' ] == 1) { |
46 | $configThumb [ 'source_image' ] = $data [ 'full_path' ]; |
47 | $this ->image_lib->initialize( $configThumb ); |
48 | $this ->image_lib->resize(); |
50 | $configLarge [ 'source_image' ] = $data [ 'full_path' ]; |
51 | $this ->image_lib->initialize( $configLarge ); |
52 | $this ->image_lib->resize(); |
57 | 'filename' => $data [ 'file_name' ], |
58 | 'albumID' => $this ->uri->segment(4,0), |
59 | 'uid' => $this ->session->userdata( 'uid' ), |
65 | $this ->load->model( 'album_model' ); |
66 | $this ->album_model->AddPic( $picture ); |
71 | $albumID = $this ->uri->segment(4); |
72 | $backurl = site_url() . 'photo/editpic/album/' . $albumID ; |
73 | $this ->session->set_flashdata( 'msg' , '图片上传成功.' ); |
74 | redirect( $backurl , 'refresh' ); |
views:new_pic.view文件:
代码如下:
01 | <form method= "post" action= "<?php echo site_url() ?>photo/upload/go/<?php echo $albumID ?>" enctype= "multipart/form-data" > |
02 | <input type= "file" name= "image1" class = "files" /> |
04 | <input type= "file" name= "image2" class = "files" /> |
06 | <input type= "file" name= "image3" class = "files" /> |
08 | <input type= "file" name= "image4" class = "files" /> |
10 | <input type= "file" name= "image5" class = "files" /> |
14 | <p><input type= "submit" name= "go" value= "上传照片" class = "button" /></p> |
此外需要注意:
1.要一次上传几个文件,修改表单和控制器中循环部分的参数就好。
2.album\\source 是上传后原图目录 large和thumb分别是两次执行$this->image_lib->resize();后存放缩略图的目录
3.缩略图文件名如需和album\\source目录一致,请添加参数 $config['thumb_marker'] = '';
4.$picture这部分数组是保存到数据库的东西,可以不用管了。