這段代碼可以:
1,同時(shí)上傳5張圖片
2,同時(shí)生成兩種尺寸的縮略圖
3,保存到mysql
controllers:upload.php
<?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
<form method="post" action="<?php echo site_url() ?>photo/upload/go/<?php echo $albumID ?>" enctype="multipart/form-data">
<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'] = '';
3,$picture這部分?jǐn)?shù)組是保存到數(shù)據(jù)庫(kù)的東西,可以不用管了。