首頁 php教程 PHP源码 一个简易的PHP文件上传类(适合与新手学习用)

一个简易的PHP文件上传类(适合与新手学习用)

May 25, 2016 pm 05:07 PM

简易PHP文件上传类

PHP代码

<?php
class Test_Upload{
 
    protected $_uploaded = array();
    protected $_destination;    
    protected $_max = 1024000;
    protected $_messages = array();
    protected $_permited = array(
                                &#39;image/gif&#39;,
                                &#39;image/jpeg&#39;,
                                &#39;image/pjpeg&#39;,
                                &#39;image/png&#39;    
    );
    protected $_renamed = false;
     
    /**
     * 
     * @param mix $path
     * 
     */
    public function __construct($path){
         
        if (!is_dir($path) || !is_writable($path)){
            throw new Exception("文件名不可写,或者不是目录!");
        }
        $this->_destination = $path;
        $this->_uploaded = $_FILES;
    }
    /**
     * 移动文件
     * 
     */
    public function move(){
         
        $filed = current($this->_uploaded);  
             
        $isOk = $this->checkError($filed[&#39;name&#39;], $filed[&#39;error&#39;]);
        //debug ok
        if ($isOk){
            $sizeOk = $this->checkSize($filed[&#39;name&#39;], $filed[&#39;size&#39;]);
            $typeOk = $this->checkType($filed[&#39;name&#39;], $filed[&#39;type&#39;]);
            if ($sizeOk && $typeOk){
                 
                $success = move_uploaded_file($filed[&#39;tmp_name&#39;], $this->_destination.$filed[&#39;name&#39;]);
                 
                if ($success){
                    $this->_messages[] = $filed[&#39;name&#39;]."文件上传成功";
                }else {
                    $this->_messages[] = $filed[&#39;name&#39;]."文件上传失败";
                }
            }
             
        }
    }
    /**
     * 查询messages数组内容 
     *
     */
    public function getMessages(){
        return $this->_messages;
    }
     
    /**
     *  检测上传的文件大小
     *  @param mix $string
     *  @param int $size
     */
    public function checkSize($filename, $size){
         
        if ($size == 0){
            return false;
        }else if ($size > $this->_max){
            $this->_messages[] = "文件超出上传限制大小".$this->getMaxsize();
            return false;
        }else { 
            return true;
        }
    }
     
    /**
     *  检测上传文件的类型
     *  @param mix $filename
     *  @param mix $type
     */
    protected  function checkType($filename, $type){
        if (!in_array($type, $this->_permited)){
            $this->_messages[] = "该文件类型是不被允许的上传类型";
            return false;
        }else {
            return true;
        }
    }
     
    /**
     *  获取文件大小
     *  
     */
    public function getMaxsize(){
        return number_format($this->_max / 1024, 1).&#39;KB&#39;;
    }
     
    /**
     * 检测上传错误
     * @param mix $filename
     * @param int $error
     * 
     */
    public function checkError($filename, $error){
        switch ($error){
            case 0 : return true;
            case 1 :
            case 2 : $this->_messages[] = "文件过大!"; return true;
            case 3 : $this->_messages[] = "错误上传文件!";return false;
            case 4 : $this->_messages[] = "没有选择文件!"; return false;
            default : $this->_messages[] = "系统错误!"; return false;
        }
    }
}
?>
登入後複製
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

Video Face Swap

Video Face Swap

使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱工具

記事本++7.3.1

記事本++7.3.1

好用且免費的程式碼編輯器

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

禪工作室 13.0.1

禪工作室 13.0.1

強大的PHP整合開發環境

Dreamweaver CS6

Dreamweaver CS6

視覺化網頁開發工具

SublimeText3 Mac版

SublimeText3 Mac版

神級程式碼編輯軟體(SublimeText3)