標籤:
<?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基礎------檔案上傳類