<?php
/**
Post Date:
06-05-21(FileUpload_Single)
06-10-7(Files_Upload)
Author:
Ray/Kaway.Cheng/zjwtnt
Email:
good999@21cn.com
FileUpload_Single:
单个文件上传类
Files_Upload:
批量上传类
它们的调用方法已标在类里边,
**/
?>
<?php
class FileUpload_Single
{
//user define -------------------------------------
var $accessPath ;//string "./" is current
var $fileSize;//int 300KB
var $defineTypeList;//string jpg|gif|bmp ...
var $filePrefix;//string
var $changNameMode;//chang name mode value: 0-2 ,NULL:不改名
//-------------------------------------------------
var $uploadFile;//array upload file attribute
var $newFileName;
var $error;
function TODO()
{//main
$this -> error = "<ul>";
$pass = true ;
if ( ! $this -> GetFileAttri() )
{
$this -> error .= "<li>The file doesn't exist.</li>";
//return false;
$pass = false;
}
if( ! $this -> CheckFileAttri_size() )
{
$this -> error .= "<li>The file uploaded is too big.</li>";
return false;
$pass = false;
}
if( ! $this -> CheckFileMIMEType() )
{
$this -> error .= "<li>The file type is incorrect.</li>";
//return false;
$pass = false;
}
if ( ! $this -> MoveFileToNewPath() )
{
$this -> error .= "<li>error, the file has been moved.</li>";
//return false;
$pass = false;
}
$this -> error .= "</ul>";
return $pass;
}
function GetFileAttri()
{ //first get the file attribute
foreach( $_FILES as $tmp )
{
$this -> uploadFile = $tmp;
}
return ( empty( $this -> uploadFile[ 'name' ] ) ) ? false : true;
}
function CheckFileAttri_size()
{
if ( ! empty ( $this -> fileSize ) )
{
if ( is_numeric( $this -> fileSize ) )
{
if ( $this -> fileSize > 0 )
{
return ( $this -> uploadFile[ 'size' ] > $this -> fileSize * 1024 ) ? false : true ;
}
}
else
{
return false;
}
}
else
{
return false;
}
}
function ChangeFileName ( $prefix = NULL , $mode )
{// string $prefix , int $mode
$fullName = ( isset( $prefix ) ) ? $prefix."_" : NULL ;
switch ( $mode )
{
case 0 : $fullName .= rand( 0 , 100 ). "_" .strtolower( date ("ldSfFYhisa") ) ; break;
case 1 : $fullName .= rand( 0 , 100 ). "_" .time(); break;
case 2 : $fullName .= rand( 0 , 10000 ) . time(); break;
case NULL : $fullName = NULL;break;
default : $fullName .= rand( 0 , 10000 ) . time(); break;
}
return $fullName;
}
function MoveFileToNewPath()
{
$newFileName = NULL;
if ( $this -> changNameMode != NULL )
{
$newFileName = $this -> ChangeFileName( $this -> filePrefix , $this -> changNameMode ). "." . $this -> GetFileTypeToString();
}
else
{
$newFileName = $this -> uploadFile[ 'name' ];
}
if ( file_exists( $this -> accessPath ) )
{
if ( file_exists( $this -> accessPath . $newFileName ) )
{
$newFileName = uniqid("")."_" . $newFileName;
echo "<script>alert('目标目录存在同名文件,因些已把上传的文件改名为:$newFileName.');</script>";
}
if ( move_uploaded_file( $this -> uploadFile[ 'tmp_name' ] , realpath( $this -> accessPath ) . "/" . $newFileName ) )
{
$this -> newFileName = $newFileName;
return true;
}
else
{
return false;
}
}
else
{
return false;
}
}
function CheckFileExist( $path = NULL )
{
return ( $path == NULL ) ? false : ( ( file_exists( $path ) ) ? true : false );
}
function GetFileMIME()
{
return empty( $this -> uploadFile[ 'type' ] ) ? NULL : $this -> uploadFile[ 'type' ] ;
}
function CheckFileMIMEType()
{
$pass = false;
$defineTypeList = strtolower( $this -> defineTypeList );
$MIME = strtolower( $this -> GetFileMIME() );
if ( !empty ( $defineTypeList ) )
{
if ( !empty ( $MIME ) )
{
foreach( explode( "|" , $MIME ) as $tmp )
{
if ( $tmp == $MIME )
{
$pass = true;
}
}
}
else
{
return false;
}
}
else
{
return false;
}
return $pass;
}
function GetFileTypeToString()
{
if( ! empty( $this -> uploadFile[ 'name' ] ) )
{
return substr( strtolower( $this -> uploadFile[ 'name' ] ) , strlen( $this -> uploadFile[ 'name' ] ) - 3 , 3 );
}
}
}
?>
<?php
/**
$tmp = new FileUpload_Single;
$tmp -> accessPath = "./" ;
$tmp -> fileSize = 50 ;//int 300KB
$tmp -> defineTypeList = "jpeg|jpg|gif";//string jpg|gif|bmp ...
$tmp -> filePrefix = "way";//string
$tmp -> changNameMode = 2;
if ( $tmp -> TODO() )
{
echo "ok";
echo "<br>".$tmp -> newFileName;
}
else
{
echo $tmp -> error;
}
**/
//echo "aa=".$_FILES[ 'myfile' ]['size'];
?>
<?php
class Files_Upload
{
//暂时不用,考虑到别的操作系统可以识别的文件类型差异,需要调试
/**
$fileType[ "exe" ] = "application/octet-stream";
$fileType[ "gif" ] = "image/gif";
$fileType[ "jpg" ] = "image/pjpeg";
$fileType[ "png" ] = "image/x-png";
$fileType[ "bmp" ] = "image/bmp";
$fileType[ "txt" ] = "text/plain";
$fileType[ "html" ] = "text/plain";
$fileType[ "doc" ] = "application/msword";
$fileType[ "xls" ] = "application/vnd.ms-excel";
$fileType[ "ppt" ] = "application/vnd.ms-powerpoint";
**/
//user define -------------------------------------
var $accessPath ;//string "./" is current
var $fileSize;//int 300KB
var $defineTypeList;//string jpg|gif|bmp ...
var $filePrefix;//string
var $changNameMode;//chang name mode value: 0-2 ,NULL:不改名
var $file_id;
//-------------------------------------------------
var $uploadFiles;//array upload file attribute
var $newFileName = array();//array
var $error = array();//array
var $fileType = array();//array
/**
$tmp = new Files_Upload;
$tmp -> accessPath = "bk/" ;
$tmp -> fileSize = 50 ;//int 300KB
$tmp -> defineTypeList = "jpeg|jpg|gif";//string jpg|gif|bmp ...
$tmp -> filePrefix = "way";//string
$tmp -> changNameMode = 2;
$tmp -> file_id = "file";//文件域id[], eg.file[]
$tmp -> TODO();
**/
function TODO()
{
$this -> GetAttri();
$this -> MoveFileToNewPath();
}
function ChangeFileName ( $prefix = NULL , $mode )
{// string $prefix , int $mode
$fullName = ( isset( $prefix ) ) ? $prefix."_" : NULL ;
switch ( $mode )
{
case 0 : $fullName .= rand( 0 , 100 ). "_" .strtolower( date ("ldSfFYhisa") ) ; break;
case 1 : $fullName .= rand( 0 , 100 ). "_" .time(); break;
case 2 : $fullName .= rand( 0 , 10000 ) . time(); break;
case NULL : $fullName = NULL;break;
default : $fullName .= rand( 0 , 10000 ) . time(); break;
}
return $fullName;
}
function GetAttri()//1
{
$this -> fileType = explode( "|" , $this -> defineTypeList );
$this -> uploadFiles = $_FILES[$this->file_id];
}
function MoveFileToNewPath()
{
$i = 0;
foreach( $this -> uploadFiles[ "name" ] as $v )
{
if ( $v != NULL )
{
if ( $this -> CheckType( $v ) )
{
if ( $this -> CheckSize( $this -> uploadFiles[ "size" ][ $i ] ) )
{
array_push( $this -> error , "$v 文件超出限制字节." );
}
else
{
$newFileName = NULL;
if ( $this -> changNameMode != NULL )
{
$newFileName = $this -> ChangeFileName( $this -> filePrefix , $this -> changNameMode ). "." . substr( $v , strrpos( $v , "." ) + 1 , strlen( $v ) - strrpos( $v , "." ) ) ;
}
else
{
$newFileName = $this -> uploadFiles[ 'name' ][ $i ];
}
if ( file_exists( $this -> accessPath ) )
{
if ( file_exists( $this -> accessPath . $newFileName ) )
{
$newFileName = uniqid("")."_" . $newFileName;
echo "<script>alert('目标目录存在同名文件,因些已把上传的文件改名为:$newFileName.');</script>";
}
if ( move_uploaded_file ( $this -> uploadFiles[ "tmp_name" ][ $i ] , realpath( $this -> accessPath ) . "/" . $newFileName ) )
{
array_push( $this -> newFileName, $newFileName );
}
else
{
array_push( $this -> error , "$v 文件不能移到目标位置." );
}
}
}
}
else
{
array_push( $this -> error , "$v 文件类型不匹配." );
}
}
$i++;
}
if ( $i == 0 ) { return false;}
}
function CheckType( $uploadName )
{
if ( in_array( substr( $uploadName , strrpos( $uploadName , "." ) + 1 , strlen( $uploadName ) - strrpos( $uploadName , "." ) ) , $this->fileType ) )
{
return true;
}
else
{
return false;
}
}
function CheckSize( $fileSize )
{
return $fileSize > $this -> fileSize * 1024;
}
}
?>