文件上传类 PHP 个人自定义版本
程序员文章站
2024-02-02 10:26:43
...
View Code
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'] = '文件' . $this->upFullName . '上传成功!'; } } } return $this->sucssInfo; } /* * (non-PHPdoc) @see Upload::errMsg() */ public function errMsg() { $errMsg = array ( 0 => '文件上传成功!', 1 => '上传文件' . $this->chkFileName . '类型错误,只支持上传' . implode ( ',', $this->upTypes ) . '等文件类型!', 2 => '上传文件' . $this->chkFileName . '太大,最大支持' . ceil ( $this->upMaxSize / 1024 ) . 'kb的文件', 3 => '上传文件' . $this->chkFileName . '超过了 php.ini 中 upload_max_filesize 选项限制的值。', 4 => '上传文件' . $this->chkFileName . '大小超过了 HTML 表单中 MAX_FILE_SIZE 选项指定的值!', 5 => '文件' . $this->chkFileName . '只有部分被上传!', 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[] = '' ) . lang_get( 'custorm_list_link' ) . ''; $t_menu_options[] = '' ) . lang_get( 'ot_link' ) . ''; $t_menu_options[] = '' ) . lang_get( 'payment_link' ) . ''; } # contact list if( !current_user_is_anonymous() ) { $t_menu_options[] = '' ) . lang_get( 'contact_list_link' ) . ''; }