Codeigniter實(shí)現(xiàn)多文件上傳并創(chuàng)建多個(gè)縮略圖
該程序可以實(shí)現(xiàn):
1.同時(shí)上傳5張圖片
2.同時(shí)生成兩種尺寸的縮略圖
3.保存到mysql
controllers:upload.php文件:
class 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;
//600*600圖片
$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();//返回上傳文件的所有相關(guān)信息的數(shù)組
$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();
//初始化600*600
$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();
}
}
/* 轉(zhuǎn)出 */
$albumID = $this->uri->segment(4);
$backurl = site_url() . 'photo/editpic/album/' .$albumID;
$this->session->set_flashdata('msg','圖片上傳成功.');
redirect($backurl,'refresh');
}
}
views:new_pic.view文件:
<input type="file" name="image1" class="files"/><br />
<input type="file" name="image2" class="files"/><br />
<input type="file" name="image3" class="files"/><br />
<input type="file" name="image4" class="files"/><br />
<input type="file" name="image5" class="files"/><br />
<br />
<p><input type="submit" name="go" value="上傳照片" class="button" /></p>
</form>
此外需要注意:
1.要一次上傳幾個(gè)文件,修改表單和控制器中循環(huán)部分的參數(shù)就好。
2.album\\source 是上傳后原圖目錄 large和thumb分別是兩次執(zhí)行$this->image_lib->resize();后存放縮略圖的目錄
3.縮略圖文件名如需和album\\source目錄一致,請(qǐng)?zhí)砑訁?shù) $config['thumb_marker'] = '';
4.$picture這部分?jǐn)?shù)組是保存到數(shù)據(jù)庫(kù)的東西,可以不用管了。
- CI框架實(shí)現(xiàn)優(yōu)化文件上傳及多文件上傳的方法
- CI框架封裝的常用圖像處理方法(縮略圖,水印,旋轉(zhuǎn),上傳等)
- CI框架文件上傳類(lèi)及圖像處理類(lèi)用法分析
- SWFUpload與CI不能正確上傳識(shí)別文件MIME類(lèi)型解決方法分享
- php基于CodeIgniter實(shí)現(xiàn)圖片上傳、剪切功能
- codeigniter上傳圖片不能正確識(shí)別圖片類(lèi)型問(wèn)題解決方法
- 2個(gè)Codeigniter文件批量上傳控制器寫(xiě)法例子
- 使用CodeIgniter的類(lèi)庫(kù)做圖片上傳
- 解決Codeigniter不能上傳rar和zip壓縮包問(wèn)題
- codeigniter教程之多文件上傳使用示例
- CodeIgniter上傳圖片成功的全部過(guò)程分享
- CI(CodeIgniter)框架實(shí)現(xiàn)圖片上傳的方法
相關(guān)文章
微信企業(yè)轉(zhuǎn)賬之入口類(lèi)分裝php代碼
這篇文章主要為大家詳細(xì)介紹了微信企業(yè)轉(zhuǎn)賬之入口類(lèi)分裝php代碼,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-10-10
thinkphp3.2實(shí)現(xiàn)在線(xiàn)留言提交驗(yàn)證碼功能
這篇文章主要為大家詳細(xì)介紹了TP3.2寫(xiě)提交的驗(yàn)證碼驗(yàn)證,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-07-07
Thinkphp調(diào)用Image類(lèi)生成縮略圖的方法
這篇文章主要介紹了Thinkphp調(diào)用Image類(lèi)生成縮略圖的方法,實(shí)例分析了Thinkphp調(diào)用Image類(lèi)生成縮略圖的使用原理與相關(guān)技巧,需要的朋友可以參考下2015-03-03
Symfony2實(shí)現(xiàn)在doctrine中內(nèi)置數(shù)據(jù)的方法
這篇文章主要介紹了Symfony2實(shí)現(xiàn)在doctrine中內(nèi)置數(shù)據(jù)的方法,結(jié)合實(shí)例形式分析了在doctrine中內(nèi)置數(shù)據(jù)的具體步驟與相關(guān)技巧,需要的朋友可以參考下2016-02-02
ThinkPHP模板中判斷volist循環(huán)的最后一條記錄的驗(yàn)證方法
這篇文章主要介紹了ThinkPHP模板中判斷volist循環(huán)的最后一條記錄的驗(yàn)證方法,需要的朋友可以參考下2014-07-07
PHP錯(cuò)誤Warning: Cannot modify header information - headers alr
這篇文章主要介紹了PHP錯(cuò)誤Warning: Cannot modify header information - headers already sent by解決方法,需要的朋友可以參考下2014-09-09

