ตัวนี้เป็น source-code ของ Hmf_Upload ของ ภายใน PHPHoffman Framework ได้ปรับแต่งมาพอสมควรจากตัวเก่า ๆ ใน v 0.1a ก่อนหน้านั้น ลดความซับซ้อนลง และปรับ Format Code ใหม่
ตัวนี้รองรับการส่งข้อมูลเข้ามาแบบหลาย ๆ ไฟล์พร้อม ๆ กัน, ดักจับ MIME Type และ Extension Type ที่ไม่ได้กำหนด รวมถึงไม่ให้ส่งไฟล์ขนาดใหญ่เกินกว่าที่กำหนดด้วย
-
<?php
-
define("HMF_UPLOAD_TXT_UPLOAD_COMPLETE", "Upload เสร็จสมบูรณ์.");
-
define("HMF_UPLOAD_TXT_UPLOAD_ERROR_SIZE", "The uploaded file exceeds the 'filesSize' setting directive.");
-
define("HMF_UPLOAD_TXT_UPLOAD_ERROR_TYPE", "รูปแบบไฟล์ผิดจากที่กำหนด");
-
define("HMF_UPLOAD_TXT_UPLOAD_ERROR_OPERATION", "Operation error on Error Messages Explained.");
-
define("HMF_UPLOAD_TXT_UPLOAD_ERR_INI_SIZE", "The uploaded file exceeds the upload_max_filesize directive.");
-
define("HMF_UPLOAD_TXT_UPLOAD_ERR_FORM_SIZE", "The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form.");
-
define("HMF_UPLOAD_TXT_UPLOAD_ERR_PARTIAL", "The uploaded file was only partially uploaded.");
-
define("HMF_UPLOAD_TXT_UPLOAD_ERR_NO_FILE", "No file was uploaded.");
-
define("HMF_UPLOAD_TXT_UPLOAD_ERR_NO_TMP_DIR", "Missing a temporary folder.");
-
define("HMF_UPLOAD_TXT_UPLOAD_ERR_EXTENSION", "File upload stopped by extension.");
-
define("HMF_UPLOAD_TXT_UPLOAD_ERR_CANT_WRITE", "Failed to write file to disk.");
-
define("HMF_UPLOAD_TXT_UPLOAD_ERR_DEFAULT", "An unknown file upload error occured.");
-
define("UPLOAD_ERR_FILE_SIZE", "-1");
-
define("UPLOAD_ERR_FILE_TYPE", "-2");
-
-
class Hmf_Upload {
-
-
private $mimeType = array();
-
private $extensionType = array();
-
private $checkUploadStatusText = array();
-
private $fileNameUploads = array();
-
private $fileSizeUploads = array();
-
private $fileMimeTypeUploads = array();
-
private $fileExtTypeUploads = array();
-
private $formInputName;
-
private $filesSize = "2097152";
-
private $desinationStoreFiles;
-
private $formBegin = false;
-
private $dirUpload;
-
private $storeFilesName = NULL;
-
-
public function SetDirUpload($value) {
-
$this->dirUpload = $value;
-
}
-
public function GetDirUpload() {
-
return $this->dirUpload;
-
}
-
public function SetInputName($value) {
-
$this->formInputName = $value;
-
}
-
public function GetInputName() {
-
return $this->formInputName;
-
}
-
public function SetFilesSize($value) {
-
$this->filesSize = $value;
-
}
-
public function GetFilesSize() {
-
return $this->filesSize;
-
}
-
public function SetAllowMimeType($mimeType) {
-
if(is_array($mimeType)) $this->mimeType = $mimeType;
-
else $this->mimeType[] = $mimeType;
-
}
-
public function GetAllowMimeType() {
-
return $this->mimeType;
-
}
-
public function SetAllowExtType($extensionType) {
-
if(is_array($extensionType)) $this->extensionType = $extensionType;
-
else $this->extensionType[] = strtolower($extensionType);
-
}
-
public function GetAllowExtType() {
-
return $this->extensionType;
-
}
-
public function SetFileNameUploads($index, $value) {
-
$this->fileNameUploads[$index] = $value;
-
}
-
public function GetFileNameUploads() {
-
return $this->fileNameUploads;
-
}
-
public function SetFileSizeUploads($index, $value) {
-
$this->fileSizeUploads[$index] = $value;
-
}
-
public function GetFileSizeUploads() {
-
return $this->fileSizeUploads;
-
}
-
public function SetFileMimeTypeUploads($index, $value) {
-
$this->fileMimeTypeUploads[$index] = $value;
-
}
-
public function GetFileMimeTypeUploads() {
-
return $this->fileMimeTypeUploads;
-
}
-
public function SetFileExtTypeUploads($index, $value) {
-
if(is_array($value)) $this->FileExtTypeUploads = $value;
-
$this->fileExtTypeUploads[$index] = strtolower($value);
-
}
-
public function GetFileExtTypeUploads() {
-
return $this->fileExtTypeUploads;
-
}
-
public function SetStoreFilesName($name) {
-
if(is_array($name)) $this->storeFilesName = $name;
-
$this->storeFilesName[] = $name;
-
}
-
public function GetStoreFilesName() {
-
return $this->storeFilesName;
-
}
-
public function SetDestinationStoreFiles($index, $value) {
-
if(is_array($value)) $this->desinationStoreFiles = $value;
-
$this->desinationStoreFiles[$index] = $value;
-
}
-
public function GetDestinationStoreFiles() {
-
return $this->desinationStoreFiles;
-
}
-
public function ActionUpload() {
-
$storeFilesName = $this->GetStoreFilesName();
-
foreach ($_FILES[$this->GetInputName()]["error"] as $key => $error) {
-
$name = $_FILES[$this->GetInputName()]["name"][$key];
-
$fileExt = $this->GetExt($name);
-
if($error != 4 ) {
-
if ($this->CheckUpload($key, $error)) {
-
if($this->CheckMimeType($_FILES[$this->GetInputName()]["type"][$key]) or $this->CheckExtType($fileExt) ) {
-
if($this->CheckSize($_FILES[$this->GetInputName()]["size"][$key])) {
-
$tmp_name = $_FILES[$this->GetInputName()]["tmp_name"][$key];
-
if(!empty($storeFilesName[$key])) $name = $storeFilesName[$key].'.'.$fileExt;
-
if(file_exists($this->GetDirUpload().'/'.$name)) unlink($this->GetDirUpload().'/'.$name);
-
move_uploaded_file($tmp_name, $this->GetDirUpload().'/'.$name);
-
$this->SetDestinationStoreFiles($key, $this->GetDirUpload().'/'.$name);
-
}
-
else {
-
$this->CheckUpload($key, UPLOAD_ERR_FILE_SIZE);
-
}
-
}
-
else {
-
$this->CheckUpload($key, UPLOAD_ERR_FILE_TYPE);
-
}
-
}
-
$this->SetFileNameUploads($key, $_FILES[$this->GetInputName()]["name"][$key]);
-
$this->SetFileSizeUploads($key, $_FILES[$this->GetInputName()]["size"][$key]);
-
$this->SetFileMimeTypeUploads($key, $_FILES[$this->GetInputName()]["type"][$key]);
-
$this->SetFileExtTypeUploads($key, $fileExt);
-
}
-
}
-
}
-
public function CheckExtType($value) {
-
$arr_type = $this->GetAllowExtType();
-
return in_array(strtolower($value), $arr_type);
-
}
-
public function CheckMimeType($value) {
-
$arr_type = $this->GetAllowMimeType();
-
return in_array($value, $arr_type);
-
}
-
public function CheckSize($value) {
-
if($value <= $this->GetFilesSize()) return true;
-
else return false;
-
}
-
public function SetUploadStatusText($index, $code, $text, $bool) {
-
$this->checkUploadStatusText[$index] = array('code' => $code, 'text'=>$text, 'bool'=>$bool);
-
}
-
public function GetUploadStatusText($index = NULL) {
-
if(empty($index)) return $this->checkUploadStatusText;
-
return $this->checkUploadStatusText[$index];
-
}
-
public function CheckUpload($index, $value) {
-
switch ($value) {
-
case UPLOAD_ERR_OK:
-
$this->SetUploadStatusText($index, $value, HMF_UPLOAD_TXT_UPLOAD_COMPLETE, true);
-
return true;
-
case UPLOAD_ERR_INI_SIZE:
-
$this->SetUploadStatusText($index, $value, HMF_UPLOAD_TXT_UPLOAD_ERR_INI_SIZE, false);
-
return false;
-
case UPLOAD_ERR_FORM_SIZE:
-
$this->SetUploadStatusText($index, $value, HMF_UPLOAD_TXT_UPLOAD_ERR_FORM_SIZE, false);
-
return false;
-
case UPLOAD_ERR_PARTIAL:
-
$this->SetUploadStatusText($index, $value, HMF_UPLOAD_TXT_UPLOAD_ERR_PARTIAL, false);
-
return false;
-
case UPLOAD_ERR_NO_FILE:
-
$this->SetUploadStatusText($index, $value, HMF_UPLOAD_TXT_UPLOAD_ERR_NO_FILE, false);
-
return false;
-
case UPLOAD_ERR_NO_TMP_DIR:
-
$this->SetUploadStatusText($index, $value, HMF_UPLOAD_TXT_UPLOAD_ERR_NO_TMP_DIR, false);
-
return false;
-
case UPLOAD_ERR_CANT_WRITE:
-
$this->SetUploadStatusText($index, $value, HMF_UPLOAD_TXT_UPLOAD_ERR_CANT_WRITE, false);
-
return false;
-
case UPLOAD_ERR_EXTENSION:
-
$this->SetUploadStatusText($index, $value, HMF_UPLOAD_TXT_UPLOAD_ERR_EXTENSION, false);
-
return false;
-
case UPLOAD_ERR_FILE_SIZE:
-
$this->SetUploadStatusText($index, -1, HMF_UPLOAD_TXT_UPLOAD_ERROR_SIZE, false);
-
return false;
-
case UPLOAD_ERR_FILE_TYPE:
-
$this->SetUploadStatusText($index, -2, HMF_UPLOAD_TXT_UPLOAD_ERROR_TYPE, false);
-
return false;
-
default:
-
$this->SetUploadStatusText($index, NULL, HMF_UPLOAD_TXT_UPLOAD_ERR_DEFAULT, false);
-
return false;
-
}
-
}
-
public function GetActionInfo() {
-
$result_details['name'] = $this->GetFileNameUploads();
-
$result_details['destination'] = $this->GetDestinationStoreFiles();
-
$result_details['size'] = $this->GetFileSizeUploads();
-
$result_details['ext'] = $this->GetFileExtTypeUploads();
-
$result_details['mime'] = $this->GetFileMimeTypeUploads();
-
$result_details['status'] = $this->GetUploadStatusText();
-
return $result_details;
-
}
-
public function GetExt($filename){
-
return strtolower(substr(strrchr($filename, '.'), 1));
-
}
-
}
-
?>
แล้วนี่ code html สำหรับทดสอบครับ
-
<form method="post" action="upload.php" enctype="multipart/form-data">
-
<input type="file" id="fdata" name="fdata[]"/>
-
<input type="file" id="fdata" name="fdata[]"/>
-
<input type="file" id="fdata" name="fdata[]"/>
-
<input type="submit" />
-
</form>
จะสังเกตว่าผมใช้ HTTP Method แบบ POST แล้วใช้ร่วมกับ Form imput แบบ Array ครับ ทำให้สามารถส่งข้อมูลหลายๆ ค่าได้ในตัว Input form ชื่อเดียวกันเลย
แล้วด้านล่างก็ code หน้าสำหรับเอาไว้ทำงาน
-
$objUpload = new Hmf_Upload();
-
$objUpload->SetDirUpload('upload');
-
$objUpload->SetStoreFilesName(array('1','2','3'));
-
$objUpload->SetInputName('fdata');
-
$objUpload->SetAllowExtType(array('jpeg','jpg','png','gif'));
-
$objUpload->ActionUpload();
-
$status = $objUpload->GetActionInfo();
SetDirUpload - กำหนด directory ปลายทาง (ไม่ต้องใส่ / ต่อท้าย)
SetStoreFilesName - กำหนดชื่อของไฟล์ที่จะเอาไปวางไว้ปลายทาง กำหนดเป็น array ตาม index ถ้ามี 3 field ก็กำหนด 3 index แต่ถ้าไม่กำหนดจะเป็นชื่อของไฟล์นั้น ๆ เลย
SetInputName - ชื่อ file field ที่เราจะให้มันส่งค่า ไม่ต้องใส่ [] ต่อท้ายครับ
SetAllowExtType - กำหนดนามสกุลที่ต้องการให้ upload ได้ ใส่เป็น array ไป
SetAllowMimeType - กำหนด Mime-type ของไฟล์ลงได้ด้วย กำหนดแบบเดียวกับ SetAllowExtType (ในตัวอย่างไม่มีเพราะดักแต่นามสกุลอย่างเดียว)
ActionUpload - สั้งให้รับค่า upload แล้วทำงาน
GetActionInfo - ข้อมูลต่าง ๆ ที่จำเป็นหลังจาก upload ครับ (ในตัวอย่างส่งไปให้กับ $status)
ข้อมูลที่ได้จากตัวแปร $status เป็น Array ครับ ลอง print_r หรือ var_dump ดูครับ
อันนี้ได้จาก var_dump
-
array(6) {
-
["name"]=> array(1) {
-
[0]=> string(12) "DSC00132.JPG"
-
}
-
["destination"]=> array(1) {
-
[0]=> string(12) "upload/1.jpg"
-
}
-
["size"]=> array(1) {
-
[0]=> int(419231)
-
}
-
["ext"]=> array(1) {
-
[0]=> string(3) "jpg"
-
}
-
["mime"]=> array(1) {
-
[0]=> string(11) "image/pjpeg"
-
}
-
["status"]=> array(1) {
-
[0]=> array(3) {
-
["code"]=> int(0)
-
["text"]=> string(44) "Upload เสร็จสมบูรณ์."
-
["bool"]=> bool(true)
-
}
-
}
-
}
ตัว source code ยังคงมี Bug บางส่วน และไม่สมบูรณ์ 100% ครับ ต้องปรับแต่งอีกบางส่วน
ตอนต่อไปเป็นการนำเอา Hmf_Upload ไปใช้คู่กับ Hmf_Img เพื่อ Scale รูป
Code support for PHP 5.2.x


0 Responses to “Class Upload (Hmf_Upload) ภายใน PHPHoffman Framework (HMF)”