PHP通用文件上传类

    由于做项目中经常遇到文件上传,因此封装了一个通用的文件上传类,作用支持组文件上传,并且不同的上传域可以上传不同的文件类型,不同的文件类型限制的文件大小也可能不相同。举例来说:用户可上传一种展品并可为该展品上传一张缩略图,那么缩略图文件限制类型可能为jpg,gif,png等,而展品文件限制类型可能为mov,avi,mpeg等,而图片大小可能限制为100KB,音频视频大小可能限制为2MB。类代码如下:


class  Upload
{
    
public   $InputName ;     //  文件上传域控件名
    
    
/* *  
    * 允许上传的文件类型
    * 形式为 array('image/jpeg', 'image/png', 'image/gif') 或包含此类数组的数组(与每个上传域控件对应)
    
*/
    
public   $FileType ;
    
    
/* *  
    * 最大上传文件大小(单位:byte)
    * 形式为 array('image' => $size, 'audio' => $size)(表示每种应用文件类型所对应的上传大小) 或包含此类数组的数组(与每个上传域控件对应)或一数值(表示所有上传文件均限制在此大小之下)
    
*/
    
public   $FileMaxSize
    
    
public   $FileSavePath ;   //  文件保存路径(可为数组形式,表示不同上传域上传文件到不同的路径)
     public   $FileSaveName ;   //  文件保存名(不包含后缀名)(可为数组形式,表示不同上传域上传文件保存的不同名称)
     public   $NoteFileFalse //  文件错误提示
     public   $NoteFileType ;   //  文件类型不符提示
     public   $NoteFileSize ;   //  文件大小超出提示
    
    
/*  上传文件并返回文件名信息(包含后缀名)  */
    
public   function  UploadFile()
    {
        
$this -> CheckFile();  //  检验文件
         $file   =   $_FILES [ $this -> InputName];
        
$file_save_full_name   =   array ();  //  文件保存名(包含后缀名)
        
        
foreach  ( $file [ ' name ' as   $key   =>   $name )
        {
            
if  ( ! empty ( $name ))  //  文件不为空
            {
                
/*  确定文件保存路径  */
                
if  ( is_array ( $this -> FileSavePath))
                {
                    
$file_save_path   =   $this -> FileSavePath[ $key ];
                }
                
else  
                {
                    
$file_save_path   =   $this -> FileSavePath;
                }
                
                
/*  确定文件保存名(不包含后缀名)  */
                
if  ( is_array ( $this -> FileSaveName))
                {
                    
$file_save_name   =   $this -> FileSaveName[ $key ];
                }
                
else  
                {
                    
$file_save_name   =   $this -> FileSaveName;
                }
                
                
/*  开始保存  */
                
$this -> CreatePath( $file_save_path );  //  如果路径不存在则创建路径
                 move_uploaded_file ( $file [ " tmp_name " ][ $key ] ,   $file_save_path   .   $file_save_name   .   $this -> GetSuffix( $file [ ' name ' ][ $key ]));
                
$file_save_full_name []  =   $file_save_name   .   $this -> GetSuffix( $file [ ' name ' ][ $key ]);
            }
            
else  
            {
                
$file_save_full_name []  =   null ;
            }
        }
        
        
unlink ( $file );
        
        
/*  如果只有一个文件,则返回单个文件名  */
        
if  ( count ( $file_save_full_name ==   1 )
        {
            
$file_save_full_name   =   $file_save_full_name [ 0 ];
        }
        
        
return   $file_save_full_name ;
    }
    
    
/*  检验文件  */
    
private   function  CheckFile()
    {
        
$file   =   $_FILES [ $this -> InputName];
        
        
foreach  ( $file [ ' name ' as   $key   =>   $name )
        {
            
if  ( ! empty ( $name ))  //  文件不为空
            {
                
$type    =   $file [ ' type ' ][ $key ];
                
$size    =   $file [ ' size ' ][ $key ];
                
$error   =   $file [ ' error ' ][ $key ];
                
                
/*  确定允许上传文件类型列表  */
                
if  ( is_array ( $this -> FileType [ 0 ]))
                {
                    
$file_type   =   $this -> FileType [ $key ];
                }
                
else  
                {
                    
$file_type   =   $this -> FileType ;
                }
                
                
/*  确定最大上传文件大小  */
                
if  ( is_array ( $this -> FileMaxSize))
                {
                    
$file_max_size_key   =   explode ( ' / ' ,   $type );
                    
$file_max_size_key   =   $file_max_size_key [ 0 ];
                    
if  ( is_array ( $this -> FileMaxSize[ 0 ]))
                    {
                        
$file_max_size   =   $this -> FileMaxSize[ $key ][ $file_max_size_key ];
                    }
                    
else  
                    {
                        
$file_max_size   =   $this -> FileMaxSize[ $file_max_size_key ];
                    }
                }
                
else  
                {
                    
$file_max_size   =   $this -> FileMaxSize;
                }
                
                
/*  文件错误  */
                
if  ( $error   >   0 )
                {
                    
die ( $name   .   $this -> NoteFileFalse);
                }
                
               
/*  文件大小超过最大上传文件大小  */
                
if  ( (! is_null ( $file_max_size &&   $size   >   $file_max_size ) || ($size == 0))
                {
                    
die ( $name   .   $this -> NoteFileSize);
                }


                    /*
 文件类型不符  */
                
if  ( ! in_array ( $type ,   $file_type ))
                {
                    
die ( $name   .   $this -> NoteFileType);
                }

            }
        }
    }
    
    
/*  获取文件后缀名  */
    
private   function  GetSuffix( $fileName )
    {
        
return   substr ( $fileName ,   strrpos ( $fileName ,   " . " ));
    }

    
/*  如果路径不存在则创建路径  */
    
private   function  CreatePath( $filePath )
    {
        
if  ( ! file_exists ( $filePath ))
        {
            
mkdir ( $filePath );
        }
    }
}


使用方法:接着以本文开头所举例子来说明该类的调用方法(呵呵,调用是很方便的):
$upload_obj = new Upload(); // 文件上传对象
$upload_obj->InputName = 'upload_test'; // 文件上传域控件名
$upload_obj->FileType = array(array('image/jpeg', 'image/png'), array('audio/mpeg', 'video/x-msvideo')); // 允许上传的文件类型
$upload_obj->FileMaxSize = array('image' => 100 * 1024, 'audio' => 2 * 1024 * 1024, 'video' => 2 * 1024 * 1024);
$upload_obj->FileSavePath = array('upload/files/s/', 'upload/files/z/');
$upload_obj->FileSaveName = time();
$upload_obj->NoteFileFalse = '文件错误';
$upload_obj->NoteFileType  = '文件类型不符';
$upload_obj->NoteFileSize  = '文件大小超出';
$file_save_full_name = $upload_obj->UploadFile(); // 上传并获取文件全名(基本名加扩展名)(如果是多个文件则为数组形式)(全名用于在数据库中存储信息)

总结:就此可轻松实现若干文件上传,其实归根结底用到了PHP组文件上传,要注意的就是控件名的name后别忘了加上[],这样的好处就是遇到多个文件上传时就不用在调用层进行循环或一个一个处理上传了,我们的应用也因此而轻松。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值