|
<? Php
/*------------*/
Class UploadFile
{
Var $ user_post_file = array ();
Var $ save_file_path = '';
Var $ max_file_size = '';
Var $ allow_type = array ('GIF', 'jpg ', 'PNG', 'Zip ', 'rar', 'txt ', 'Doc', 'PDF ');
Var $ final_file_path = '';
Var $ save_info = array ();
Var $ error_info = array ();
/**
* Constructor, used to initialize information.
*
* @ Param Array $ file
* @ Param String $ path
* @ Param Integer $ size
* @ Param Array $ type
*/
Function _ construct ($ file, $ path, $ size = 2097152, $ type = '')
{
$ This-> user_post_file = $ file;
$ This-> save_file_path = $ path;
$ This-> max_file_size = $ size;
If (! $ Type = ''){
$ This-> allow_type [] = $ type;
}
}
/**
*
*
* @ Access public
* @ Return int
*/
Function upload ()
{
For ($ I = 0; $ I <count ($ this-> user_post_file ['name']); $ I ++)
{
If ($ this-> user_post_file ['error'] [$ I] = 0) {// The status of the uploaded file is normal
// Obtain the current file name, temporary file name, size, type, and extension.
$ Name = $ this-> user_post_file ['name'] [$ I];
$ Tmp_name = $ this-> user_post_file ['tmp _ name'] [$ I];
$ Size = $ this-> user_post_file ['size'] [$ I];
$ Type = $ this-> user_post_file ['type'] [$ I];
$ Ext_name = $ this-> getExtName ($ name );
// File size
If (! $ This-> checkSize ($ size )){
$ This-> error_info [] = 'File you uploaded: '. $ name.' too large ';
Continue;
}
// Extension
If (! $ This-> checkType ($ ext_name )){
$ This-> error_info [] = 'File you uploaded: '. $ name.' invalid ';
Continue;
}
// Illegal Upload
If (! Is_uploaded_file ($ tmp_name )){
$ This-> error_info [] = 'File you uploaded: '. $ name.' illegal submission ';
Continue;
}
//
$ Basename = $ this-> getBaseName ($ name, ".". $ ext_name );
$ Final_filename = $ basename. '-'. time (). '-'. rand (1,10000). '.'. $ ext_name;
$ This-> final_file_path = $ this-> save_file_path. '/'. $ final_filename;
If (! Move_uploaded_file ($ tmp_name, $ this-> final_file_path )){
$ This-> error_info = $ this-> user_post_file ['error'] [$ I];
Continue;
}
//
$ This-> save_info [] = array (
"Name" => $ name,
"Ext_name" => $ ext_name,
"Type" => $ type,
"Size" => $ size,
"Final_filename" => $ final_filename,
"Path" => $ this-> final_file_path
);
}
}
Return count ($ this-> save_info );
}
/*
* Check whether the size of the uploaded file is valid.
*
* @ Param Integer $ size
* @ Access private
* @ Return boolean
*/
Function checkSize ($ size)
{
If ($ size> $ this-> max_file_size ){
Return FALSE;
}
Return TRUE;
}
/*
* Check whether the object type uploaded by the user is valid.
*
* @ Access private
* @ Return boolean
*/
Function checkType ($ extension)
{
Foreach ($ this-> allow_type as $ type ){
If (strcasecmp ($ extension, $ type) = 0 ){
Return TRUE;
}
}
Return FALSE;
}
/*
* Get the file extension
*
* @ Param string $ filename
* @ Access private
* @ Return string
*/
Function getExtName ($ filename)
{
$ P = pathinfo ($ filename );
Return $ p ['extension'];
}
/*
* Get the file name (excluding the extension)
*
* @ Param string $ filename
* @ Param string $ type
* @ Access private
* @ Return boolean
*/
Function getBaseName ($ filename, $ ext_name)
{
$ Basename = basename ($ filename, $ ext_name );
Return $ basename;
}
/*
*
*
*
*/
Function showErrorInfo ()
{
If (count ($ this-> error_info )! = 0 ){
// Echo 'error... <br/> ';
Foreach ($ this-> error_info as $ k => $ v ){
Echo ($ k + 1), ':', $ v, '<br/> ';
}
}
}
Function getSaveInfo ()
{
Return $ this-> save_info;
}
}
// $ Upload = new UploadFile ('','');
// $ Upload = new UploadFile ();
// $ Upload-> showErrorInfo ();
?> |