<?php
/**
*文件上傳類 Upload
*/
class Upload {
const UPLOAD_SECCUSS = "上傳成功!請進入下一步..." ;
const UPLOAD_FAILING = "上傳失敗!" ;
const UPLOAD_ERRORS = "上傳圖片失敗,是圖片文件爲空、文件格式不正確,或是文件超過了限大小" ;
const NOT_UPLOAD_FILE = "不是上傳的文件,你想幹嗎!!" ;
var $id ; //id關聯編號
var $dirStyle = 0 ; //設置文件上傳目錄方式0:mdir方式 ;1 :mdir_YM 方式 ; 2:mdir_YM_D 方式
var $size ; //文件大小 單位:字節
var $type ; //文件類型
var $path ; //保存迴路徑
var $upfile ; //上傳文件數組
//var $name ; //form中 "type=file" 輸入框的name值
var $isVaild=false ; //驗證
var $isSuccess=false ; //是否成功
//var $newpath ; // 已處理過的路徑
var $newfilename ; //已處理過的文件名php
const MY_DS = '/'; //路徑分隔符 通用的
function __construct($upfile,$path,$type="image/pjpeg,image/jpg,image/jpeg,image/gif,image/x-png,image/png,image/bmp",$size=50000){
$this->path = $path ;
$this->type = explode(',',$type) ;
$this->size = $size ;
$this->upfile = $upfile ;
}
/**
*用id號關聯文件
*/
function setId($id){
$this->id = $id ;
}
/**
*設置文件上傳目錄方式
*/
function setDirStyle($id=1){
if($id == 1){
return ;
} else {
$this->dirStyle = 2 ;
}
}
/**
*驗證文件是否能夠上傳
*/
function vaild(){
foreach( $this->type as $value ){
if( $this->upfile['type'] == $value && $this->upfile['size'] > 0 &&
$this->upfile['size'] <= $this->size ){
$this->isVaild = true ;
return true ;
}
}
return $this->isVaild ;
}
/**
*判斷是否上傳成功
*/
function isSuccess(){
return $this->isSuccess ;
}
/**
*分年月創建日錄
*/
function mdir_YM($path){
//$dir = date("Y-m") ;
$dir = date("Ym") ;
$path = str_replace(array('\'),self::MY_DS,$path) ;
if( substr($path,-1,1) != self::MY_DS ){
$path .= self::MY_DS ;
}
if(! file_exists($path.$dir )){
if( mkdir($path.$dir) ){
$this->path= $path.$dir.self::MY_DS ;
}
}else{
$this->path= $path.$dir.self::MY_DS ;
}
}
/**
*分年月/日創建日錄
*/
function mdir_YM_D($path){
//$dir = date("Y-m") ;
$dir = date("Ym") ;
$subdir = date("d") ;
$path = str_replace(array('\'),self::MY_DS,$path);
if ( substr($path,-1,1) != self::MY_DS ){
$path .= self::MY_DS ;
}
$dirPath = $path.$dir ;
$subDirPath = $dirPath.self::MY_DS.$subdir ;
if (! file_exists( $dirPath )){
if ( mkdir( $dirPath ) ){
if (file_exists($dirPath ) && !file_exists( $subDirPath )){
if (mkdir( $subDirPath )){
$this->path= $subDirPath.self::MY_DS ;
}
}
}
} else if (!file_exists( $subDirPath )) {
if (mkdir( $subDirPath )){
$this->path= $subDirPath.self::MY_DS ;
}
} else {
$this->path = $subDirPath.self::MY_DS ;
}
}
/**
*根據路徑建立目錄
*@param $path 帶文件名的路和徑
*/
static function mdirPath($path){
$thePath = str_replace(array('\'),self::MY_DS,$path);
$thePath = dirname(substr_compare($thePath,self::MY_DS,0,1) == 0 ? substr($thePath,1) : $thePath );
$sub = explode(self::MY_DS,$thePath) ;
$n = count($sub) ;
$i = 0 ;
$tempPath = '';
while($i < $n){
$tempPath .= $sub[$i++].self::MY_DS ;
if (!file_exists($tempPath)) {
if (! mkdir($tempPath)) {
return false ;
}
}
}
if($i >= $n){
return true;
}
}
/**
*(static)
*分年月/日創建日錄
*/
static function sMdir_YM_D($path){
//$dir = date("Y-m") ;
$dir = date("Ym") ;
$subdir = date("d") ;
$path = str_replace(array('\'),self::MY_DS,$path) ;
if( substr($path,-1,1) != self::MY_DS ){
$path .= self::MY_DS ;
}
$dirPath = $path.$dir ;
$subDirPath = $dirPath.self::MY_DS.$subdir ;
if (! file_exists( $dirPath )){
if ( mkdir( $dirPath ) ){
if (file_exists( $dirPath ) && !file_exists( $subDirPath )){
if (mkdir( $subDirPath )){
return $subDirPath.self::MY_DS ;
} else {
return '' ;
}
} else {
return $subDirPath.self::MY_DS ;
}
} else {
return '' ;
}
}else if (!file_exists( $subDirPath )){
if (mkdir( $subDirPath )){
return $subDirPath.self::MY_DS ;
} else {
return '' ;
}
} else {
return $subDirPath.self::MY_DS ;
}
}
/**
*返回文件路徑名,不包括文件名
*/
function getPath(){
return $this->path;
}
/**
*返回文件保存的完整路徑
*/
function getFullPath(){
return (substr_compare($this->path,self::MY_DS,0,1) != 0 ? self::MY_DS.$this->path : $this->path).$this->newfilename;
}
/**
*產生新的文件名
*/
function mfilename($name){
$names = explode(".",$name);
$theId = !empty($this->id) ? $this->id.'_' : '' ;
$this->newfilename= $theId.uniqid(rand()).".".$names[count($names)-1];
}
/**
*返回新的文件名
*/
function getNewFilename(){
return $this->newfilename;
}
/**
*開始上傳
*/
function doUpload(){
if($this->vaild()){
if(is_uploaded_file($this->upfile['tmp_name'])){
if($this->dirStyle == 1){
$this->mdir_YM($this->path);
} else if($this->dirStyle == 2) {
$this->mdir_YM_D($this->path);
} else {
//$this->path = substr_compare($this->path,self::MY_DS,0,1) != 0 ? self::MY_DS.$this->path : $this->path ;
}
$this->mfilename($this->upfile['name']);
if (is_uploaded_file($this->upfile['tmp_name'])){
if(move_uploaded_file($this->upfile['tmp_name'],$this->path.$this->getNewFilename())){
$msg = self::UPLOAD_SECCUSS ;
$this->isSuccess = true;
}else{
$msg = self::UPLOAD_FAILING ;
}
}
} else {
$msg = self::NOT_UPLOAD_FILE ;
}
}else{
$msg = self::UPLOAD_ERRORS. round($this->size/1024) ."KB." ;
}
return $msg ;
}數組
}
?>this