又花了半天时间写了个php上传类解决方法

php中文网
发布: 2016-06-13 13:31:05
原创
903人浏览过

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

PHP code
<!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

-->
//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(保存到服务器的文件夹);
     * $isRename(对上传文件重命名,值 有 y|n );
     * $reType(上传成功后返回的值:n是返回文件名,pn:返回路径和文件名,j:返回js[待扩展]);
     * $upExt(要上传的文件分类,因为在不同的表单要限制不同的上传文件类型,比如 A表单只能上传图片,B表单只能上伟压缩包);
     * $maxSize(限制上传的文件大小)
     * @author:256kb
     * @2012-5-1
     */
    public function __construct($inputFile , $upExt = 0 , $reType = 'pn' , $upFolder = 'upload/' , $isRename = 'y' , $maxSize = 10485760){
        $this-&gt;inputFile = $inputFile;
        $this-&gt;reType = $reType;
        $this-&gt;upMaxSize = $maxSize;
        $this-&gt;allowFile = $upExt;
        $this-&gt;upFolder = $upFolder;
        $this-&gt;isReName = $isRename;
        //$this-&gt;errorInt = -1;
    }
    
    public function upFile(){
        $_file_arr = $_FILES[$this-&gt;inputFile];
        $this-&gt;errorInt = $_file_arr['error'];
        if(is_uploaded_file($_file_arr['tmp_name'])){
            if($_file_arr['tmp_name']){
                $this-&gt;tmpName = $_file_arr['name'];
                $this-&gt;upMaxSize = $_file_arr['size'];
                $this-&gt;fileType = $_file_arr['type'];
                $this-&gt;tmpPath = $_file_arr['tmp_name'];
                $this-&gt;fileSize = $_file_arr['size'];
                if($this-&gt;upMaxSize &gt; $this-&gt;upMaxSize){
                    $this-&gt;errorInt = 6    ;    //大小超出网站限制
                }
                
                if(!$this-&gt;isAllow()){
                    $this-&gt;errorInt = 8    ;    //系统不允许此类型文件
                }
                
                if($this-&gt;isReName=='y'){
                    $this-&gt;savePath = $this-&gt;upFolder.$this-&gt;getFolder().'/'.$this-&gt;getNewName() ;
                    $this-&gt;endFileName = $this-&gt;getNewName();
                }else{
                    $this-&gt;savePath = $this-&gt;upFolder.$this-&gt;getFolder().'/'.$this-&gt;tmpName ;
                    $this-&gt;endFileName = $this-&gt;tmpName;
                }
                //echo $this-&gt;errorInt;
                if(!$this-&gt;errorInt &gt;= 1){
                    move_uploaded_file($this-&gt;tmpPath,$this-&gt;savePath);
                }
                
            }
        }
    }
    
    public function getFileUrl(){
        switch($this-&gt;reType){
            case 'n':
                return $this-&gt;endFileName;
            break;
            case 'pn':
                return $this-&gt;savePath;
            break;
            case 'js':
                return "<script language='\"javascript\"' type='\"text/javascript\"'>window.parent.LoadAttach('".$this->savePath."');</script>";
            break;
            default:
                return $this-&gt;savePath;
        }
    }
    
    //获得新文件名
    public function getNewName(){
        return substr($this-&gt;tmpName,1,strrpos($this-&gt;tmpName,".")-1).'_'.mktime().'.'.$this-&gt;getFileExt();
    }
    
    public function upStatus(){
        //echo $this-&gt;errorInt;
        switch ($this-&gt;errorInt){
            case 1:
                return '超过了文件大小php.ini中限制大小';
            break;
            case 2:
                return '超过了文件大小MAX_FILE_SIZE 选项指定的值';
            break;
            case 3:
                return '文件只有部分被上传';
            break;
            case 4:
                return '没有文件被上传';
            break;
            case 5:
                return '上传文件大小为0';
            break;
            case 6:
                return '大小超出网站限制';
            break;
            case 7:
                return '网站内没有指定这种上传类型';
            break;
            case 8:
                return '系统不允许此类型文件';
            case 9:
                return '创建目录失败!';
            break;
        }
    }
    
    private function isAllow(){
        $allow = array(
                0 =&gt; array('image/jpg','image/jpeg','image/png','image/pjpeg','image/gif','image/bmp','image/x-png','application/x-zip-compressed','application/octet-stream'),
                1 =&gt; array('image/jpg','image/jpeg','image/png','image/pjpeg','image/gif','image/bmp','image/x-png'),
                2 =&gt; array('application/x-zip-compressed','application/octet-stream'),
                3 =&gt; array('','',''),
                4 =&gt; array('','','')
                );
        if($this-&gt;allowFile &gt; count($allow)-1){
            $this-&gt;errorInt = 7;    //网站内没有指定这种上传类型
        }else{
            if(in_array($this-&gt;fileType, $allow[$this-&gt;allowFile])){
                return true;
            }else{
                return false;
            }
        }
    }
    
    public function getFileExt(){    //获得文件扩展名
        return strtolower(substr($this-&gt;tmpName,strrpos($this-&gt;tmpName,".")+1));
    }
    
    private function getFolder(){    //获得并自动创建相应文件夹
        if(strpos('|rar|zip|7z|iso|','|'.$this-&gt;getFileExt().'|')&gt;=0){
            $_folder = 'rar';
        }elseif(strpos('|gif|jpeg|jpg|png|bmp|pjpeg|psd|','|'.$this-&gt;getFileExt().'|')&gt;=0){
            $_folder = 'img';
        }elseif(strpos('|rm|rmvb|avi|mp4|swf|flv|wmv|','|'.$this-&gt;getFileExt().'|')&gt;=0){
            $_folder = 'vide';
        }elseif(strpos('|doc|txt|xls|mdb||','|'.$this-&gt;getFileExt().'|')&gt;=0){
            $_folder = 'doc';
        }else{
            $_folder = 'other';
        }
        if(!file_exists($this-&gt;upFolder.$_folder)){
            if(!mkdir($this-&gt;upFolder.$_folder)){
                $this-&gt;errorInt = 9;    //创建目录失败
            }
        }
        return $_folder;
    }
    
}


 <div class="clear"></div>
登录后复制
PHP速学教程(入门到精通)
PHP速学教程(入门到精通)

PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!

下载
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号