您的位置:首页 > 其它

基于Phalcon的文件上传封装

2015-12-08 09:51 555 查看
分享一段在工作中封装的上传文件类库,使用都是非常的方便,好啦,话不多说,直接上代码。

<?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());
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: