Codeigniter实现多文件上传并创建多个缩略图
前端之家收集整理的这篇文章主要介绍了
Codeigniter实现多文件上传并创建多个缩略图,
前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
@H_301_0@该程序可以实现:
1.同时上传5张图片
2.同时生成两种尺寸的缩略图
3.保存到MysqL
@H_
301_0@
controllers:upload.PHP文件:
@H_
301_0@<div class="codetitle">
<a style="CURSOR: pointer" data="94834" class="copybut" id="copybut94834" onclick="doCopy('code94834')"> 代码如下: <div class="codebody" id="code94834"><?
PHPclass Upload extends Controller {
function go() {
if(isset($_POST['go'])) {
//初始化
$config['upload_path'] = 'album/source';
$config['allowed_types'] = 'gif|jpg|png|bmp|jpeg';
$config['encrypt_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图片
$configThumb = array();
$configThumb['image_library'] = 'gd2';
$configThumb['source_image'] = '';
$configThumb['create_thumb'] = TRUE;
$configThumb['maintain_ratio'] = TRUE; //保持图片比例
$configThumb['new_image'] = 'album/thumb';
$configThumb['width'] = 170;
$configThumb['height'] = 170;
//600600
图片 $configLarge = array();
$configLarge['image_library'] = 'gd2';
$configLarge['source_image'] = '';
$configLarge['create_thumb'] = TRUE;
$configLarge['maintain_ratio'] = TRUE; //保持
图片比例
$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();//返回
上传文件的所有相关信息的数组
$uid = $this->session->userdata('uid');
$uploadedFiles[$i] = $data; if($data['is_image'] == 1) {
//初始化170
170
$configThumb['source_image'] = $data['full_path']; //文件路径带文件名
$this->image_lib->initialize($configThumb);
$this->image_lib->resize();
//初始化600600
$configLarge['source_image'] = $data['full_path']; //
文件路径带
文件名
$this->image_lib->initialize($configLarge);
$this->image_lib->resize();
} //插入
图片信息到album表,插入的
文件名为source目录
文件名
$picture = array(
'filename' => $data['file_name'],
'albumID' => $this->uri->segment(4,0),
'uid' => $this->session->userdata('uid'),
'dateline' => time(),
'describe' => '',
'click' => 0
); $this->load->model('album_model');
$this->album
model->AddPic($picture);
$picture = array();
}
}
@H301_0@ /
转出 /
$albumID = $this->uri->segment(4);
$backurl = site_url() . 'photo/editpic/album/' .$albumID;
$this->session->set_flashdata('msg','
图片上传成功.');
redirect($backurl,'refresh');
}
}