ใครใช้ Twitter ก็ follow ผมได้ที่ FordAntiTrust นะครับ ![]()
Archive for January, 2008
ตัวนี้เป็น source-code ของ Hmf_Upload ของ ภายใน PHPHoffman Framework ได้ปรับแต่งมาพอสมควรจากตัวเก่า ๆ ใน v 0.1a ก่อนหน้านั้น ลดความซับซ้อนลง และปรับ Format Code ใหม่
ตัวนี้รองรับการส่งข้อมูลเข้ามาแบบหลาย ๆ ไฟล์พร้อม ๆ กัน, ดักจับ MIME Type และ Extension Type ที่ไม่ได้กำหนด รวมถึงไม่ให้ส่งไฟล์ขนาดใหญ่เกินกว่าที่กำหนดด้วย
CODE:
-
<?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 สำหรับทดสอบครับ
CODE:
-
<form method="post" action="upload.php" enctype="multipart/form-data">

