基于Phalcon的文件上传封装
2015-12-08 09:51
555 查看
分享一段在工作中封装的上传文件类库,使用都是非常的方便,好啦,话不多说,直接上代码。
如何调用上传类呢?
Upload分页一般可以放到library目录中,或者任意注册过的目录中,然后就可以在控制器中直接调用了,调用的示例如下:
<?php /** * 文件上传 * User: Kp * Date: 2015/11/23 * Time: 13:48 */ class Upload{ // request控制器对象 private $request; // 是否自动创建目录 private $auto_mkdir = true; // 文件上传路径 private $file_path; // 上传文件最大尺寸 private $file_size; // 上传文件的格式限制 private $file_type; // file文件对象 private $file; // 错误信息 private $error_info; // 错误状态,默认无错误 private $error_state = false; // 上传成功后文件真实路径 private $file_realpath = array(); public function __construct($request , $file_path , $file_size , $file_type){ $this->request = $request; $this->file_path = $file_path; $this->file_size = $file_size; $this->file_type = $file_type; } /** * 获取文件真实保存路径 * @return array */ public function getFileRealPath(){ return $this->file_realpath; } /** * 执行上传文件 * @return bool */ public function uploadfile(){ // 检测是否有上传文件 if ($this->request->hasFiles() == true) { // 获取上传文件的相关信息 foreach ($this->request->getUploadedFiles() as $file) { $this->file = $file; // 检查文件大小 $this->checkSize(); // 检查文件类型 $this->checkType(); // 移动文件到指定目录 $this->move(); } }else{ $this->error_state = true; $this->error_info = '暂无上传文件'; } } /** * 移动文件 * @return bool */ private function move(){ if(!$this->error_state){ // 检查并创建目录 $this->checkDir(); // 目录路径 $filepath = trim($this->file_path , '/') . '/'; // 生成文件名称 $filename = $this->getFileName(); // 移动文件 if(!$this->file->moveTo($filepath . $filename)){ $this->error_state = true; $this->error_info = '上传文件失败'; }else{ $this->file_realpath[] = $filepath . $filename; } } } /** * 生成文件名规则 * @return string */ private function getFileName(){ return date('YmdHis') . uniqid() . '.' . $this->file->getExtension(); } /** * 检查并创建目录 * @return bool */ private function checkDir(){ if(!$this->error_state){ if(!is_dir($this->file_path)){ if($this->auto_mkdir){ if(!$this->mkdir($this->file_path)){ $this->error_state = true; $this->error_info[] = '目录创建失败'; } }else{ $this->error_state = true; $this->error_info[] = '上传目录不存在'; } } } } /** * 检测上传文件的大小 * @return bool */ private function checkSize(){ if(!$this->error_state){ if($this->file->getSize() > $this->file_size){ $this->error_state = true; $this->error_info[] = '上传文件过大'; } } } /** * 递归创建目录 * @param $dir * @return bool */ private function mkdir($dir){ if(!is_dir($dir)){ if(!$this->mkdir(dirname($dir))){ return false; } if(!mkdir($dir,0777)){ return false; } } return true; } /** * 检查上传文件类型 * @return bool */ private function checkType(){ if(!$this->error_state){ if(is_array($this->file_type)){ if(!in_array($this->file->getExtension() , array_map('strtolower' , $this->file_type))){ $this->error_state = true; $this->error_info[] = '文件类型错误'; } }else if(is_string($this->file_type)){ if(strtolower($this->file->getExtension()) != strtolower($this->file_type)){ $this->error_state = true; $this->error_info[] = '文件类型错误'; } }else{ $this->error_state = true; $this->error_info[] = '文件类型错误'; } } } /** * 返回错误信息 * @return mixed */ public function errInfo(){ return $this->error_info; } /** * 返回错误状态 * @return bool */ public function errState(){ return $this->error_state; } }
如何调用上传类呢?
Upload分页一般可以放到library目录中,或者任意注册过的目录中,然后就可以在控制器中直接调用了,调用的示例如下:
/** * 图片基础上传控件 */ public function uploadAction(){ // 定义上传路径 $file_savepath = './upload/images/'; // 定义上传文件的大小 $file_size = 2 * 1024 * 1024; // 定义上传文件的允许类型 $file_type = array('jpg','png','gif'); // 上传配置 $upload = new Upload($this->request , $file_savepath , $file_size , $file_type); // 开始上传 $upload->uploadfile(); // 判断上传状态 true标识没有上传成功 false 标识上传成功 if(!$upload->errState()){ // 返回文件保存真实路径 return array(1 , $upload->getFileRealPath()); }else{ // 打印错误信息 return array(0 , $upload->errInfo()); } }
相关文章推荐
- Hiding Header Views
- leetcode Reverse Bits
- ubuntu unrar解压rar包和unzip 解压zip包
- LeetCode 208 Implement Trie (Prefix Tree)
- 曲径通幽—关于Visual Stdio 自定义向导框的构建(1)
- webservice与接口开发以及phprpc,构建php服务器
- html之页面元素印射
- javaweb开发-问题笔记
- 《C和指针》章节后编程练习解答参考——第9章
- 如何从SAP中连接其他数据库
- QT中使用快速排序
- webservice并行能力及配置
- tomcat绿色版及安装版修改内存大小的方法
- mysql
- 设计模式之:行为型设计模式(11种)
- ASP.NET Cookie概念、CURD操作、原理、实际运用
- Android使用wheelView实现简单类似ios PickerView选择器效果
- 图的邻接表实现_LGraph
- 全局程序集缓存(GAC)中 安装程序集 ADODB 版本
- MIGO时没发料不允许入库