频道栏目
首页 > 资讯 > CodeIgniter > 正文

codeigniter框架文件上传处理

16-01-08        来源:[db:作者]  
收藏   我要投稿

前几天介绍过CodeIgniter 框架input表单的重新填充,主要是针对text、radio、checkbox、select等input表单,那么对于文件上传表单file该如何处理呢?

自己的处理方式:

//设置文件上传属性
$webroot = $_SERVER['DOCUMENT_ROOT'];
$time    = time();
$year    = date('Y', $time);
$month   = date('m', $time);
$day     = date('d', $time);
$subpath = "/goods/coverimage/{$year}/{$month}/{$day}/";
$path    = $webroot . '/uploads' . $subpath;
if(!file_exists($path))
{
	mkdir($path, 0777, true);
}
$config['upload_path']   = $path;
$config['allowed_types'] = 'jpg|gif|png';
$config['file_name']     = date('YmdHis', $time) . mt_rand(100, 999);
$this->load->library('upload', $config);

if($this->upload->do_upload('coverimage'))
{
	$file           = $this->upload->data();
	$data['image0'] = $subpath . $file['orig_name'];

	if($this->goods_model->add_goods($data))
	{
		$this->success(base_url() . 'admin.php?c=goods', '添加商品成功', 2);
	}
}
else //图片上传失败
{
	$msg               = array();
	$msg['file_error'] = strip_tags($this->upload->display_errors());
	$this->view('goods/modify', $msg);
}
从上面的代码可以看到用到了图片上传的3个函数:
$this->upload->do_upload('coverimage')

$this->upload->data()

$this->upload->display_errors()
通过判断图片是否上传成功,来控制获取图片信息或输出相应的错误信息。
相关TAG标签
上一篇:Codeigniter出现A Database Error Occurred错误
下一篇:Codeigniter出现Unable to connect to your database server using the provided settings错误解决办法
相关文章
图文推荐

关于我们 | 联系我们 | 广告服务 | 投资合作 | 版权申明 | 在线帮助 | 网站地图 | 作品发布 | Vip技术培训 | 举报中心

版权所有: 红黑联盟--致力于做实用的IT技术学习网站