日期:2014-05-17  浏览次数:20461 次

又花了半天时间写了个php上传类
刚学PHP没多久,为了更好的练习及熟悉PHP,自己花了大半天写了个php上传类,在这里做个笔记,欢迎朋友们对这个类做修改及优化。

PHP code

//up_class.php

<?php
class UpFile{
    private $inputFile;    //文件域名
    private $tmpName;    //临时文件名
    private $tmpPath;    //临时文件路径
    private $savePath;    //保存路径
    private $reType;    //返回类型
    private $upMaxSize;    //上传文件大小限制
    private $allowFile; //允许上传的文件
    private $upFolder; //要上传到的文件夹
    private $isReName;    //是否要将上传的文件重命名
    private $endFileName; //最终保存的文件名
    public  $fileSize;    //文件大小
    public  $fileType;    //文件类型
    public  $errorInt;    //上传失败及错误原因
    
    
    /**
     * @param 
     * $inputFile(表单内文件域名称);</br>
     * $upFolder(保存到服务器的文件夹);</br>
     * $isRename(对上传文件重命名,值 有 y|n );</br>
     * $reType(上传成功后返回的值:n是返回文件名,pn:返回路径和文件名,j:返回js[待扩展]);</br>
     * $upExt(要上传的文件分类,因为在不同的表单要限制不同的上传文件类型,比如 A表单只能上传图片,B表单只能上伟压缩包);</br>
     * $maxSize(限制上传的文件大小)</br>
     * @author:256kb
     * @2012-5-1
     */
    public function __construct($inputFile , $upExt = 0 , $reType = 'pn' , $upFolder = 'upload/' , $isRename = 'y' , $maxSize = 10485760){
        $this->inputFile = $inputFile;
        $this->reType = $reType;
        $this->upMaxSize = $maxSize;
        $this->allowFile = $upExt;
        $this->upFolder = $upFolder;
        $this->isReName = $isRename;
        //$this->errorInt = -1;
    }
    
    public function upFile(){
        $_file_arr = $_FILES[$this->inputFile];
        $this->errorInt = $_file_arr['error'];
        if(is_uploaded_file($_file_arr['tmp_name'])){
            if($_file_arr['tmp_name']){
                $this->tmpName = $_file_arr['name'];
                $this->upMaxSize = $_file_arr['size'];
                $this->fileType = $_file_arr['type'];
                $this->tmpPath = $_file_arr['tmp_name'];
                $this->fileSize = $_file_arr['size'];
                if($this->upMaxSize > $this->upMaxSize){
                    $this->errorInt = 6    ;    //大小超出网站限制
                }
                
                if(!$this->isAllow()){
                    $this->errorInt = 8    ;    //系统不允许此类型文件
                }
                
                if($this->isReName=='y'){
                    $this->savePath = $this->upFolder.$this->getFolder().'/'.$this->getNewName() ;
                    $this->endFileName = $this->getNewName();
                }else{
                    $this->savePath = $this->upFolder.$this->getFolder().'/'.$this->tmpName ;
                    $this->endFileName = $this->tmpName;
                }
                //echo $this->errorInt;
                if(!$this->errorInt >= 1){
                    move_uploaded_file($this->tmpPath,$this->savePath);
                }
                
            }
        }
    }
    
    public function getFileUrl(){
        switch($this->reType){
            case 'n':
                return $this->endFileName;
            break;
            case 'pn':
                return $this->savePath;
            break;
            case 'js':
                return "<script language=\"javascript\" type=\"text/javascript\">window.parent.LoadAttach('".$this->savePath."');</script>";
            break;
            default:
                return $this->savePath;
        }
    }
    
    //获得新文件名
    public function getNewName(){
        return substr($this->tmpName,1,strrpos($this->tmpName,".")-1).'_'.mktime().'.'.$this->getFileExt();
    }
    
    public function upStatus(){
        //echo $this->errorInt;
        switch ($this->errorInt){
            case 1:
                return '超过了文件大小php.ini中限制大小';
            break;
            case 2: