雷林鹏分享:codeigniter框架文件上传处理

  前几天介绍过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()

  通过判断图片是否上传成功,来控制获取图片信息或输出相应的错误信息。

  文章转载自 [http://www.php230.com]

  (编辑:雷林鹏 来源:网络 侵删)

posted @ 2019-05-09 14:14  lei林鹏  阅读(160)  评论(0编辑  收藏  举报