關於上傳圖片的類(有點粗糙)

來源:互聯網
上載者:User

標籤:io   ar   sp   檔案   on   cti   bs   ad   as   

<?php
class Upload
{
    private $doc;            //檔案
    private $docsize;        //檔案大小
    private $docname;        //檔案名稱字
    private $doctype;        //檔案類型
    public $docnewname;        //圖片新名字
    private $allowtype;        //可以上傳的檔案類型
    private $seterror=1;    //錯誤原因
    private $doctmpname;    //檔案臨時名
    private $path;            //檔案儲存體路徑
    function __construct($file,$path)
    {
        $this->doc=$file;
        $this->path=$path;
        $this->docsize=$file[‘size‘];
        $this->docname=$file[‘name‘];
        $this->doctmpname=$file[‘tmp_name‘];
        $array=explode(‘.‘,$this->docname);
        $allowtype=Array("jpg","jpeg","gif","png","bmp");
        $this->doctype=$array[count($array)-1];
        $this->docnewname=time().".".$this->doctype;
        $error=$file[‘error‘];
        if($error!=0)
        {
            switch($error)
            {
                case 1:$this->seterror="上傳的檔案過大,最大能上傳2M";break;
                case 2:$this->seterror="上傳的檔案過大,最大能上傳2M";break;
                case 3:$this->seterror="檔案只有部分被上傳";break;
                case 4:$this->seterror="檔案沒有被上傳";break;
                case 6:$this->seterror="找不到臨時檔案夾";break;
                case 7:$this->seterror="檔案寫入失敗";break;
            }
        }
        else
        {
            if(!in_array($this->doctype,$allowtype))
            {
                $this->seterror="檔案類型不符";
            }
            else
            {
                if(!is_uploaded_file($this->doctmpname))
                {
                    $this->seterror="非法操作";
                }
                else
                {
                    if(!file_exists($this->path))
                    {
                        mkdir($this->path,07777);
                    }
                    else
                    {
                        if(!move_uploaded_file($this->doctmpname,$this->path.$this->docnewname))
                        {
                            $this->seterror="檔案上傳失敗";
                        }
                    }
                }
            }
        }
    }
    function get_img_newname()
    {
        if($this->seterror!=1)
        {
            echo $this->seterror;exit;
        }
        else
        {
            return $this->path.$this->docnewname;
        }
    }
}

?>

關於上傳圖片的類(有點粗糙)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.