PHP手寫的圖片上傳類

來源:互聯網
上載者:User

一個新手寫的 PHP 圖片上傳類,基本的功能已經滿足,可以返回用戶端檔案名稱,檔案大小,臨時檔案夾路徑,允許上傳的檔案類型,檔案尾碼,新路徑,新檔案名稱,圖片寬度,圖片高度,自訂上傳成功訊息,顯示上傳的狀態。。是否上傳成功的資訊等等,外加建構函式,對於新手學習來說,已經足夠了,值得研究學習。。

在使用時,和其它PHP類一樣,先要進行初始化對象,在前台調用函數方法即可,這裡不再詳細說明,這個類的研究價值主要在於代碼的規範與實用,並沒有大量繁瑣的代碼,都是一些簡單的上傳函數整合成的一個PHP圖片上傳類,下面是詳細的類代碼:

 代碼如下 複製代碼

class upload {
private $file_name = ‘null’; #用戶端檔案名稱
private $file_size = 0; #檔案大小
private $file_tmpName = ”; #臨時檔案夾路徑
private $upload_type = array(‘jpg’,'jpeg’,'gif’,'png’); #允許上傳的檔案類型
private $upload_Ext = ‘null’; #檔案尾碼
private $upload_dir = ‘null’; #新路徑
private $upload_name = ‘null’; #新檔案名稱
private $upload_width = 0; #圖片寬度
private $upload_height = 0; #圖片高度
private $upload_msg = ”; #上傳訊息
private $upload_mode; #上傳狀態
/**
* 建構函式,自動上傳
* @access public
* @param string $control 檔案域控制項名
* @param string $filePath 儲存路徑
* @param string $fileName 新檔案名稱,不帶尾碼
* @param array $fileType 允許上傳的檔案尾碼
* @param int $size 允許上傳的檔案大小,以位元組為單位
*/
public function __construct($control=”,$filePath=”,$fileName=”,$fileType=”,$size=0) {
$this->upload_mode = true;
if($control==”)
$this->error(‘上傳的檔案域控制項名不可為空’);
else{
$this->file_name=$_FILES[$control]['name'];
$this->file_size=$_FILES[$control]['size'];
$this->file_tmpName=$_FILES[$control]['tmp_name'];
if($fileType!=”)
$this->upload_type = $fileType;
if($filePath==”)
$this->upload_dir=’./’;
else
$this->upload_dir=$filePath;
if(!is_uploaded_file($this->file_tmpName))
$this->error(‘沒有檔案被上傳’);
else{
if($size!=0&&$this->file_size>$size)
$this->error(‘上傳的圖片檔案不能大於’.$this->getSize($size));
if(!$this->typeDetect($this->file_name))
$this->error(‘不支援上傳此類型的檔案’);
else{
if(!$this->fileDetect($this->file_tmpName))
$this->error(‘上傳的圖片檔案無效或已經損壞’);
}
if($fileName==”)
$this->upload_name=$this->file_name;
else
$this->upload_name=$fileName.’.’.$this->upload_Ext;
}
}
//開始儲存檔案
if($this->mode)
$this->saveFile();
}
/**
* 擷取屬性
* @access public
* @param string 屬性名稱
* @return 屬性值
*/
public function __get($name){
switch($name){
case ‘path’:
return $this->upload_dir;
break;
case ‘name’:
return $this->upload_name;
break;
case ‘ext’:
return $this->upload_Ext;
break;
case ‘size’:
return $this->getSize($this->file_size);
case ‘width’:
return $this->upload_width;
break;
case ‘height’:
return $this->upload_height;
break;
case ‘mode’:
return $this->upload_mode;
break;
case ‘msg’:
return $this->upload_msg;
break;
}
}
/**
* 檢測檔案尾碼名
* @access public
* @return bool
*/
private function typeDetect($OldFile){
$tempArr = explode(“.”, $OldFile);
$fileExt = array_pop($tempArr);
$fileExt = trim($fileExt);
$fileExt = strtolower($fileExt);
if(in_array($fileExt,$this->upload_type)){
$this->upload_Ext=$fileExt;
return true;
}
else
return false;
}
/**
* 檢測上傳檔案是否是有效圖片格式
* @access private
* @return bool
*/
private function fileDetect($tmpName){
$arr = getimagesize($tmpName);
if(!$arr){
return false;
}else{
$this->upload_width = $arr[0];
$this->upload_height = $arr[1];
return true;
}
}
/**
* 儲存檔案
* @access private
*/
private function saveFile(){
if(!is_dir($this->upload_dir)){
mkdir($this->upload_dir, 0777);
chmod($this->upload_dir, 0777);
}
$save_file = move_uploaded_file($this->file_tmpName,$this->upload_dir.$this->upload_name);
if(!$save_file)
$this->error(‘檔案寫入失敗’);
else
$this->upload_msg=’檔案上傳成功’;
}
/**
* 擷取檔案大小
* @return string 返回B,KB,MB單位
*/
private function getSize($tmpSize){
$value = ‘B’;
if($tmpSize>1024){
$tmpSize = floor($tmpSize/1024);
$value = ‘KB’;
}
if($tmpSize>1024){
$tmpSize = round($tmpSize/1024,2);
$value = ‘MB’;
}
return $tmpSize.$value;
}
/**
* 錯誤提示
* @access private
* @param string $msg 錯誤資訊
*/
private function error($msg=”){
$this->upload_msg .= $msg;
$this->upload_mode = false;
}
}

聯繫我們

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