初出茅廬的圖片上傳的封裝-- -- --物件導向

來源:互聯網
上載者:User

標籤:io   ar   os   sp   for   檔案   on   art   cti   

//今天才學的  留作紀念吧

//upload.php

<?php
    class Upload
    {
        private $filename;//檔案名稱
        private $type;//檔案類型
        private $fileerror;//檔案錯誤
        private $allowsize;//允許的檔案大小
        private $allowtype;//允許的類型
        private $path;//路徑
        private $newsname;//檔案新名稱
        private $tmpname;//快取檔案名稱
        private $filetypes;//尾碼名
        private $seterror;//錯誤
        function __construct($files,$path)
        {
            $this->filename=$files[‘name‘];
            $types=explode(".",$this->filename);//根據.截取檔案的尾碼名稱(.jpg)
            $this->filetypes=$types[count($types)-1];//尾碼名稱(jpg)
            $this->fileerror=$files[‘error‘];
            $this->allowtype=array("jpg","png","gif","bmp","jpeg");
            $this->allowsize=2000000;
            $this->path=$path;//路徑
            $this->newsname=time().".".$this->filetypes;
            $this->tmpname=$files[‘tmp_name‘];//快取檔案
            $this->seterror=1;
            if($this->fileerror!=0)
            {
                switch($this->fileerror)
                {
                    case 1:
                        echo "檔案大小超出限制";exit;break;
                    case 2:
                        echo "上傳檔案過大";exit;break;
                    case 3:
                        echo "檔案只有部分上傳";exit;break;
                    case 4:
                        echo "檔案沒有被上傳";exit;break;
                    case 6:
                        echo "找不到臨時檔案夾";exit;break;
                    case 7:
                        echo "檔案寫入失敗";exit;break;
                }
            }
            else
            {
                if(!file_exists($this->path))
                {
                    mkdir($this->path,0777);
                }
                else
                {
                    if(!is_uploaded_file($this->tmpname))
                    {
                        echo "非法操作";exit;
                    }
                    else
                    {
                        if($this->seterror!=1)
                        {
                            echo $this->seterror;exit;
                            
                        }
                        else
                        {
                            move_uploaded_file($this->tmpname,$this->path.$this->newsname);
                        }
                    }
                }
            }
        }
        function get_path_name()
        {
            return $this->path.$this->newsname;
        }
    }
?>

 

 

//do_upload.php頁面

<meta charset="utf-8">
<form method="post" action="do_upload.php" enctype="multipart/form-data">
<input type="hidden" name="MAX_FILE_SIZE" value="200000"/>
<input type="file" name="files" value="瀏覽"/>
<input type="submit" name="sub" value="上傳"/>
</form>
<?php
    if(isset($_POST[‘sub‘]))
    {
        include "upload.php";
        $up=new Upload($_FILES[‘files‘],"up/");
        $imgname=$up->get_path_name();
    
?>
<img src="<?php echo $imgname ?>"/>
<?php
}
?>

初出茅廬的圖片上傳的封裝-- -- --物件導向

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.