|
本帖最后由 Closer 于 2015-2-11 10:00 编辑
发一个多文件上传以及缩略图的方法!有些粗略,但功能还是可以的,菜鸟就这程度,见笑!
控制器:Controller-》
PHP复制代码 class Uploads extends CI_Controller {
public function __construct (){
parent ::__construct ();
//开启文件上传类
$this->load->helper(array('form', 'url'));
}
/**
*显示文件上传的方法
*/
public function home (){
$prefs = array (
'show_next_prev' => TRUE,
'next_prev_url' => site_url ().'/uploads/add_img/',
'day_type' =>'short',
);
$this->load->library('calendar', $prefs);
echo $this->calendar->generate($this->uri->segment(3), $this->uri->segment(4));
$data['select'] = $this->db->where('pro_id','28')->get('pro_thumb')->result_array();
$this->load->view('uploads',$data);
}
/*
*文件上传操作方法
*/
public function action_upload (){
//新增产品信息
if($_POST){
$data = array(
'pro_name'=>$this->input->post('pro_name'),
);
$data['add'] = $this->db->insert('pro_data',$data);
if($data['add'] && $data['add']!=''){
$pid = mysql_insert_id();//返回上一次插入的ID值
}
}
//创建大缩略图文件夹
if(!file_exists('./uploads/big_thumb/'.date('Y/m/d'))){
mkdir('./uploads/big_thumb/'.date('Y/m/d'), 0777, true);
}
//创建小缩略图文件夹
if(!file_exists('./uploads/small_thumb/'.date('Y/m/d'))){
mkdir('./uploads/small_thumb/'.date('Y/m/d'), 0777, true);
}
$config['upload_path'] = "./uploads/images/";//文件保存目录;
$config['allowed_types'] = 'gif|jpg|png|jpeg';//限制文件类型
$config['max_siza'] = '5120';//限制文件大小,以字节为单位
$config['encrypt_name'] = true;//开启文件重命名
$this->load->library('upload',$config);
//for循环多文件与缩略图
for($i=1; $i<=count($_FILES);$i++){
if($this->upload->do_upload('img'.$i)){
$upload_data = $this->upload->data();//返回图片信息
//生成大缩略图
$config_big_thumb['image_library'] = 'gd2';
$config_big_thumb['source_image'] = $upload_data['full_path'];//原图来源
$config_big_thumb['new_image'] = './uploads/big_thumb/'.date('Y/m/d');//保存目录
$config_big_thumb['create_thumb'] = TRUE;
$config_big_thumb['maintain_ratio'] = TRUE;
$config_big_thumb['width'] = 250;
$config_big_thumb['height'] = 250;
$config_big_thumb['thumb_marker'] = '_250_250';
//生成小缩略图
$config_small_thumb['image_library'] = 'gd2';
$config_small_thumb['source_image'] = $upload_data['full_path'];//原图来源
$config_small_thumb['new_image'] = './uploads/small_thumb/'.date('Y/m/d');//保存目录
$config_small_thumb['create_thumb'] = TRUE;
$config_small_thumb['maintain_ratio'] = TRUE;
$config_small_thumb['width'] = 100;
$config_small_thumb['height'] = 100;
$config_small_thumb['thumb_marker'] = '_100_100';
//载入图片处理类库
$this->load->library('image_lib');
$this->image_lib->initialize($config_big_thumb);//引用大缩略图
$this->image_lib->resize();//生成缩略图
$this->image_lib->initialize($config_small_thumb);//引用小缩略图
$this->image_lib->resize();//生成缩略图
//插入原图
$data = array(
'image'=>$upload_data['file_name'],
'pro_id'=>$pid,
);
$data['add_img'] = $this->db->insert('pro_img',$data);
if($data['add_img'] && $data['add_img']!=''){
//插入缩略图
$data = array(
'thumb'=>$upload_data['raw_name'].'_250_250'.$upload_data['file_ext'],
'pro_id'=>$pid,
);
$data['thumb_marker'] = $this->db->insert('pro_thumb',$data);
if(!$data['thumb_marker'] && $data['thumb_marker']==''){
echo '<script type="text/javascript">alert("上传缩略图失败!");location.href="'.site_url ('uploads/home').'";</script>';
}else{
echo '<script type="text/javascript">location.href="'.site_url ('uploads/home').'";</script>';
}
}
}
}
}
} 复制代码
视图:view-》
HTML复制代码 <form method="post" action="<?php echo site_url('uploads/action_upload')?>" enctype="mulitipart/form-data">
<p><input type="text" name="pro_name"></p>
<p><input type="file" name="img1"></p>
<p><input type="file" name="img2"></p>
<p><input type="file" name="img3"></p>
<p><input type="submit" name="上传"></p>
</form>
复制代码
upload.rar
(1.13 MB, 下载次数: 67)
|
评分
-
查看全部评分
|