php图片上传类

001 <?php
002 class FileUpload_Single
003 {
004 //user define -------------------------------------
005 var $accessPath ;
006 var $fileSize=200;
007 var $defineTypeList="jpg|jpeg|gif|bmp";//string jpg|gif|bmp  ...
008 var $filePrefix"useruplod_";//上传后的文件名前缀,可设置为空
009 var  $changNameMode;//图片改名的规则,暂时只有三类,值范围 : 0 至 2 任一值
010 var $uploadFile;//array upload file attribute
011 var $newFileName;
012 var $error;
013  
014 function TODO()
015 {//main 主类:设好参数,可以直接调用
016 $pass = true ;
017 if ( ! $this -> GetFileAttri() )
018 {
019    $pass = false;
020 }
021 if( ! $this -> CheckFileMIMEType() )
022  {
023  $pass = false;
024  $this -> error .= die("<script language=\"javascript\">alert('图片类型不正确,允许格式:jpg|jpeg|gif|bmp。');history.back()</script>");
025 }
026  
027 if( ! $this -> CheckFileAttri_size() )
028 {
029    $pass = false;
030    $this -> error .= die("<script language=\"javascript\">alert('上传的文件太大,请确保在200K以内。');history.back()</script>");
031    return false;
032 }
033    
034 if ( ! $this -> MoveFileToNewPath() )
035 {
036     $pass = false;
037     $this -> error .=  die("<script language=\"javascript\">alert('上传失败!文件移动发生错误!');history.back()</script>");
038
039   return $pass;
040 }
041 function GetFileAttri()
042 {
043   foreach$_FILES as $tmp )
044   {
045    $this -> uploadFile = $tmp;
046   }
047   return (empty$this -> uploadFile[ 'name' ])) ? false : true;
048 }
049   
050 function CheckFileAttri_size()
051 {
052   if ( ! empty $this -> fileSize ))
053   {
054    if is_numeric$this -> fileSize ))
055    {
056     if ($this -> fileSize > 0)
057     {
058      return ($this -> uploadFile[ 'size' ] > $this -> fileSize * 1024) ? false : true ;
059     }  
060    }
061    else
062    {
063     return false;
064    }
065   }
066   else
067   {
068    return false;
069   }
070  }
071  function ChangeFileName ($prefix = NULL  , $mode)
072  {// string $prefix , int $mode
073   $fullName = (isset($prefix)) ? $prefix."_" : NULL ;
074   switch ($mode)
075   {
076    case 0   : $fullName .= rand( 0 , 100 ). "_" .strtolower(date ("ldSfFYhisa")) ; break;
077    case 1   : $fullName .= rand( 0 , 100 ). "_" .time(); break;
078    case 2   : $fullName .= rand( 0 , 10000 ) . time();   break;
079    default  $fullName .= rand( 0 , 10000 ) . time();   break;
080   }
081   return $fullName;
082  }
083  function MoveFileToNewPath()
084  {
085   $newFileName = NULL;
086   $newFileName $this -> ChangeFileName( $this -> filePrefix , 2 ). "." $this -> GetFileTypeToString();
087   //检查目录是否存在,不存在则创建,当时我用的时候添加了这个功能,觉得没用的就注释掉吧
088   /*
089   $isFile = file_exists( $this -> accessPath);
090   clearstatcache();
091    if( ! $isFile && !is_dir($this -> accessPath) )
092    {
093        echo $this -> accessPath;
094     @mkdir($this -> accessPath);
095    }*/
096 $array_dir=explode("/",$this -> accessPath);//把多级目录分别放到数组中
097  for($i=0;$i<count($array_dir);$i++){
098   $path .= $array_dir[$i]."/";
099   if(!file_exists($path)){
100    mkdir($path);
101   }
102  }
103 /
104     if ( move_uploaded_file( $this -> uploadFile[ 'tmp_name' ] , realpath$this -> accessPath ) . "/" .$newFileName ) )
105     {
106         $this -> newFileName = $newFileName;
107             return true;
108     }else{
109         return false;
110     }
111 /
112 }
113 function CheckFileExist( $path = NULL)
114  {
115   return ($path == NULL) ? false : ((file_exists($path)) ? true : false);
116  }
117 function GetFileMIME()
118  {
119   return $this->GetFileTypeToString();
120  }
121 function CheckFileMIMEType()
122  {
123   $pass = false;
124   $defineTypeList strtolower$this ->defineTypeList);
125   $MIME strtolower$this -> GetFileMIME());
126   if (!empty ($defineTypeList))
127   {
128    if (!empty ($MIME))
129    {
130     foreach(explode("|",$defineTypeListas $tmp)
131     {
132      if ($tmp == $MIME)
133      {
134       $pass = true;
135      }
136     }
137    }
138    else
139    {
140     return false;
141    }     
142    }
143    else
144    {
145    return false;
146    }
147    return $pass;
148  }
149   
150  function GetFileTypeToString()
151  {
152   if( ! empty$this -> uploadFile[ 'name' ] ) )
153   {
154    return substrstrtolower$this -> uploadFile[ 'name' ] ) , strlen$this -> uploadFile[ 'name' ] ) - 3 , 3 ); 
155   }
156  }
157 }
158 ?>

以下是PHP上传类的调用方法,PHP代码如下:

01 <?php
02 include 'up.class.php';//加载PHP上传类文件
03 if (empty($HTTP_POST_FILES['image_file']['tmp_name']))//判断接收数据是否为空
04 {
05         $tmp new FileUpload_Single;
06         $tmp -> accessPath ='upload';//图片上传的目录,这里是当前目录下的upload目录,可自己修改
07         if $tmp -> TODO() )
08         {
09             $filename=$tmp -> newFileName;//生成的文件名
10             echo "图片上传成功,路径为:upload/".$filename;
11         }else{
12             echo $tmp -> error;
13         }          
14 }
15 else{
16     echo "没有图片数据可上传";
17 }
18 ?>

原作地址链接  http://blog.csdn.net/qq1355541448/article/details/42710017

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值