PHP图片上传类

  class  uploadPic
    {
        
private   $imgtype ;
        
private   $mimetype ;
        
private   $path ;
        
private   $error ;
        
        
public   $picname ;
        
public   $picwidth ;
        
public   $picheight ;
        
// 返回函数
         function  goback( $str )
        {
            
echo   " <script>alert(' " . $str . " ');history.go(-1);</script> " ;
            
exit ();
        }
        
function  check( $file ) // 检查文件类型
        {
            
if ( $_FILES [ $file ][ " name " ==   "" )
                
$this -> goback( " 没有文件要上传 " );
            
$types   =   array ( " image/gif " , " image/jpeg " , " image/pjpeg " , " image/png " );
            
if ( ! in_array ( $_FILES [ $file ][ " type " ] , $types ))
                
$this -> goback( " 请用jpg/gif/png格式的图片 " );
            
if ( $_FILES [ $file ][ " size " >   300000 )
                
$this -> goback( ' 对不起,图片大小不能超过300k,请用photoshop压缩或换一幅小的图片! ' );
            
            
$this -> mimetype  =   $_FILES [ $file ][ " type " ];
            
$size   =   getimagesize ( $_FILES [ $file ][ ' tmp_name ' ]);
            
$this -> picwidth  =   $size [ 0 ];
            
$this -> height  =   $size [ 1 ];
            
$this -> path  =   $_FILES [ $file ][ " tmp_name " ];
            
$this -> error  =   $_FILES [ $file ][ " error " ];
            
$this -> getType ();
        }
        
function   getType () // 返回文件扩展名
        {
            
switch ( $this -> mimetype)
            {
                
case   " image/gif " :
                    
$this -> imgtype  =   " .gif " ;
                    
break ;
                
case   " image/jpeg " :
                    
$this -> imgtype  =   " .jpg " ;
                    
break ;
                
case   " image/pjpeg " :
                    
$this -> imgtype  =   " .jpg " ;
                    
break ;
                
case   " image/png " :
                    
$this -> imgtype  =   " .png " ;
                    
break ;
            }
        }
        
function  getError()
        {
            
switch ( $this -> error)
            {
                
case   0 :
                    
break ;
                
case   1 :
                    
$this -> goback( " 文件大小超过限制,请缩小后再上传 " );
                    
break ;
                
case   2 :
                    
$this -> goback( " 文件大小超过限制,请缩小后再上传 " );
                    
break ;
                
case   3 :
                    
$this -> goback( " 文件上传过程中出错,请稍后再上传 " );
                    
break ;
                
case   4 :
                    
$this -> goback( " 文件上传失败,请重新上传 " );
                    
break ;
            }
        }
        
function  upfile( $path , $file )
        {
            
$this -> check( $file );
            
// $this->goback($this->mimetype);
             $this -> picname  =   date ( ' YmdHis ' ) . rand ( 0 , 9 );
            
$this -> getError();
            
move_uploaded_file ( $this -> path , $path . $this -> picname . $this -> imgtype);
            
            
$this -> picname  =   $this -> picname . $this -> imgtype;
        }
    }
    
    
$pic   =   new  uploadPic;
    
$pic -> upfile( " uploadpics/ " , " compic " ); // 第一个参数是要保存在服务器上的路径,第二个参数是上传控件的名字
     $compicname   =   $pic -> picname; // 上传后的图片名字,
 
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值