代码改变世界

文件上传类 PHP 个人自定义版本

2012-03-11 00:09  Brave Cheng  阅读(1368)  评论(2编辑  收藏  举报
View Code
<?php
/**
* 上传接口
* @author BraveCheng
*
*/
abstract class Upload {
public $upName; // 文件名称
public $upMaxSize = 8142; // 文件上传大小
public $upPath; // 上传路径
public $upTypes = array (
'jpg',
'gif',
'png'
); // 文件类型
public $time; // 文件上传时间或者自定义上传目录
public $destFolder; // 目标文件夹
public $upError; // 错误代号
public $sucssInfo = array (); // 成功日志
abstract protected function fileUpload();
abstract protected function errMsg(); // 错误日志
}

/**
* 文件上传类
*
* @author BraveCheng
*
*/
class CustomUpload extends Upload {
protected $upFullPathName; // 上传之后的完整路径名称
protected $upFullName; // 上传之后的完整名称
protected $fileField; // 上传文件名称空间,如$_FILES['field']['name'].
protected $chkFileExt; // 上传文件名称后缀
protected $chkFileSize; // 上传文件实际大小
protected $chkFileName; // 上传文件名称
/**
*
* @param 自定义路径 $custPath
* @param 自定义名称 $custName
* @param 自定义类型 $custTypes
* @param 自定义最大尺寸 $custMaxSize
*/
function __construct($custDir, $custPath, $custName, $custTypes = array(), $custMaxSize, $fileField) {
$this->destFolder = $custDir; // 自定义上传的文件路径
$this->upPath = $custPath;
$this->upName = $this->setName ( $custName );
$this->upTypes = $custTypes;
$this->upMaxSize = $custMaxSize;
$this->fileField = ( string ) $fileField;
$this->time = time (); // 初始化主要用来统一文件名称和目录
}

/**
* 重命名上传文件,支持中文名
*
* @param string $custName
* @return string
*/
private function setName($custName) {
return ! empty ( $custName ) ? iconv ( "utf-8", "gbk", $custName ) : date ( 'YmdHis', $this->time ) . mt_rand ( 10, 99 );
/*
* if ($custName == '') { // 如果未设置文件名,则生成一个随机文件名 $name = date (
* 'YmdHis',$this->time ) . "_" . mt_rand ( 10, 99 ) . '.' . $this->ext;
* // 判断文件是否存在,不允许重复文件 if (file_exists ( $this->savePath . $name )) {
* $name = setSavename ( $saveName ); } } else { $name = $saveName; }
* $this->saveName = $name; }
*/
}

private function setPath() {
return (preg_match ( '/\/$/', $this->upPath )) ? $this->upPath : $this->upPath . '/';
}
/**
* 创建目录
*
* @param string $baseDir
* @param string $destDir
*/
private function mkDirs($baseDir, $destDir) {
$dirs = $baseDir;
! is_dir ( $dirs ) && @mkdir ( $dirs, 0777 ); // 原来如果前面的假是正的。后面的语句就执行
if (! empty ( $destDir )) {
$destDirs = explode ( '/', $destDir );
foreach ( $destDirs as $finalDir ) {
! empty ( $finalDir ) && $dirs .= $finalDir . '/';
! is_dir ( $dirs ) && @mkdir ( $dirs, 0777 );
}
} else {
$dirs .= date ( 'Ymd', $this->time ) . '/';
! is_dir ( $dirs ) && @mkdir ( $dirs, 0777 );
}
return $dirs;
}

/**
* 获得后缀函数
*
* @param string $fileName
* @return mixed
*/
private function getFileExt($filename) {
$extend = pathinfo ( $filename );
$this->chkFileExt = $extend ['extension'];
}

/**
* 检测文件后缀函数
*
* @return boolean
*/
private function checkFileExt() {
if (in_array ( $this->chkFileExt, $this->upTypes )) { // 此处程序有bug
return TRUE;
} else {
$this->upError = 1;
return FALSE;
}
}

/**
* 检测最大尺寸
*
* @return boolean
*/
private function checkFileMaxSize() {
if ($this->chkFileSize > $this->upMaxSize) {
$this->upError = 2;
return FALSE;
}
return TRUE;
}
/*
* (non-PHPdoc) @see Upload::fileUpload()
*/
public function fileUpload() {
// 单文件、多文件上传
$keys = array_keys ( $_FILES [$this->fileField] ['name'] );
foreach ( $keys as $key ) {
if (! $_FILES [$this->fileField] ['name'] [$key])
continue;
$sysError = $_FILES [$this->fileField] ['error'] [$key];
switch ($sysError) {
case 1 :
$this->upError = 3;
break;
case 2 :
$this->upError = 4;
break;
case 3 :
$this->upError = 5;
break;
case 4 :
$this->upError = 6;
break;
case 5 :
$this->upError = 7;
break;
}
$this->chkFileName = iconv ( "utf-8", "gbk", $_FILES [$this->fileField] ['name'] [$key] ); // 循环中的文件名称
$this->chkFileSize = $_FILES [$this->fileField] ['size'] [$key]; // 循环中的文件大小
$this->getFileExt ( $this->chkFileName );
// 文件类型检测
if (! $this->checkFileExt ()) {
return $this->errMsg ();
exit ();
}
// 超过大小
if (! $this->checkFileMaxSize ()) {
return $this->errMsg ();
exit ();
}
if ($sysError == 0 && is_uploaded_file ( $_FILES [$this->fileField] ['tmp_name'] [$key] )) {
// 组装文件名称
/*
* $upFullPathName = $this->upName . $key . '.' .
* $this->chkFileExt; // 不允许重复 if (file_exists ( $upFullPathName
* )) { $this->upFullName = $upFullPathName; }
*/
$this->upFullName = $this->upName . $key . '.' . $this->chkFileExt;
$this->upFullPathName = $this->mkDirs ( $this->destFolder, $this->setPath () ) . $this->upFullName;
if (move_uploaded_file ( $_FILES [$this->fileField] ['tmp_name'] [$key], $this->upFullPathName )) {
$this->sucssInfo ['name'] = $this->upFullPathName;
$this->sucssInfo ['size'] = $this->chkFileSize;
$this->sucssInfo ['info'] = '文件<font color=red>' . $this->upFullName . '</font>上传成功!';
}
}
}
return $this->sucssInfo;
}
/*
* (non-PHPdoc) @see Upload::errMsg()
*/
public function errMsg() {
$errMsg = array (
0 => '文件上传成功!',
1 => '上传文件<font color=red>' . $this->chkFileName . '</font>类型错误,只支持上传<font color=red>' . implode ( ',', $this->upTypes ) . '</font>等文件类型!',
2 => '上传文件<font color=red>' . $this->chkFileName . '</font>太大,最大支持<font color=red>' . ceil ( $this->upMaxSize / 1024 ) . '</font>kb的文件',
3 => '上传文件<font color=red>' . $this->chkFileName . '</font>超过了 php.ini 中 upload_max_filesize 选项限制的值。',
4 => '上传文件<font color=red>' . $this->chkFileName . '</font>大小超过了 HTML 表单中 MAX_FILE_SIZE 选项指定的值!',
5 => '文件<font color=red>' . $this->chkFileName . '</font>只有部分被上传!',
6 => '没有文件被上传。',
7 => '文件上传失败!'
);
if ($this->upError == 0)
return false;
else
return $errMsg [$this->upError];
}
}

发布下自己定义的上传类!环境新人和高手拍砖!

 

# custom define page
          if( access_has_global_level( config_get( 'manage_site_threshold' ) ) ) {
               $t_menu_options[] = '<a href="' . helper_mantis_url( 'custorm_define_list_page.php">' ) . lang_get( 'custorm_list_link' ) . '</a>';
               $t_menu_options[] = '<a href="' . helper_mantis_url( 'ot_application_page.php">' ) . lang_get( 'ot_link' ) . '</a>';
               $t_menu_options[] = '<a href="' . helper_mantis_url( 'payment_application_page.php">' ) . lang_get( 'payment_link' ) . '</a>';
          }

          # contact list
          if( !current_user_is_anonymous() ) {
               $t_menu_options[] = '<a href="' . helper_mantis_url( 'view_contact_list.php">' ) . lang_get( 'contact_list_link' ) . '</a>';
          }

  

QQ: 249636292 mail: chenghuiyong1987@gmail.com Blog: http://www.cnblogs.com/bravecheng My Friend,We are!