PHP基礎------檔案上傳類

來源:互聯網
上載者:User

標籤:

<?php//檔案上傳類$upImage = $_FILES["img"];//Array//(//    [name] => gou.jpg//    [type] => image/jpeg//    [tmp_name] => D:\wamp\tmp\php311F.tmp//    [error] => 0//    [size] => 9488//)class upLoad{    private $_arr;    //建構函式,把映像資訊賦值給$_arr欄位    public function __construct($arrImg) {        $this->_arr = $arrImg;    }    //得到副檔名    private function getExtName()    {        //explode 以.把字串切割為數組       $extName = explode(".", $this->_arr["name"]);       //取得數組最後一個元素然後返回       return $extName = end($extName);    }    //判斷檔案大小    private function checkSize()    {        if($this->_arr["size"]>2000000)        {            exit("圖片不能大於2M");        }    }    //檢測圖片類型 jpg gif png    private function checkType($extName)    {        if($extName!="jpg" && $extName!="gif" && $extName!="png")        {            exit("圖片格式不對,請重新上傳!");        }    }    //為檔案重新命名    private function getFileName()    {        //定義檔案名稱        $filePathName = "Uploads/";        //判斷上傳檔案夾是否存在        if(!is_dir($filePathName))        {           //建立目錄            mkdir($filePathName);         }        //判斷日期檔案夾是否存在        $filePathTime = $filePathName.date("Y-m-d")."/";        if(!is_dir($filePathTime))        {             //建立目錄            mkdir($filePathTime);        }        //檔案重新命名        $fileName = $filePathTime.date("Ymd").rand(1000, 9999).‘.‘.$this->getExtName();        return $fileName;    }        //檔案上傳主調方法    public function Main()    {        //獲得副檔名        $eName = $this->getExtName();        //判斷檔案大小        $this->checkSize();        //檢測檔案類型        $this->checkType($eName);        //檔案重新命名        $fileName = $this->getFileName();        //上傳檔案        move_uploaded_file($this->_arr["tmp_name"],$fileName);        //返迴文件路徑,資料庫儲存需要        return $fileName;    }    }$up = new upLoad($upImage);$up->Main();

 

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.