Ex.
Upload.class.php
<?php
class MyUpload{
var $cls_upload_dir = ""; // Directory to upload to.
var $cls_filename = ""; // Name of the upload file.
var $cls_tmp_filename = ""; // TMP file Name Upload.
var $cls_arr_ext_accepted = array(".gif", ".jpg", ".jpeg", ".png" );
var $cls_file_rename_to = ''; // New name for the file after upload.
function MyUpload($file_name, $tmp_file_name,
$file_size, $file_rename_to = ''){
$this->cls_filename = $file_name;
$this->cls_tmp_filename = $tmp_file_name;
$this->cls_filesize = $file_size;
$this->cls_file_rename_to = $file_rename_to;
}
function isUploadedFile(){
if(is_uploaded_file( $this->cls_tmp_filename) != true){
return "IS_UPLOADED_FILE_FAILURE";
} else {
return 1;
}
}
function checkExtensionDir($dir){
if(!is_writable($dir) ){
return "DIRECTORY_FAILURE";
} else {
$this->cls_upload_dir = $dir;
return 1;
}
}
function checkExtension(){
if(!in_array(strtolower(strrchr($this->cls_filename, ".")),
$this->cls_arr_ext_accepted)){
return "EXTENSION_FAILURE";
} else {
return 1;
}
}
function move(){
if(move_uploaded_file($this->cls_tmp_filename,
$this->cls_upload_dir . $this->cls_filename) == false){
return "MOVE_UPLOADED_FILE_FAILURE";
} else {
return 1;
}
}
function checkFileExists(){
if(file_exists($this->cls_upload_dir . $this->cls_filename) ){
return "FILE_EXISTS_FAILURE";
} else {
return 1;
}
}
function upload( $dir ){
$ret = $this->checkExtension(); //ตรวจสอบนามสกุลไฟล์
if( $ret != 1 ){
return $this->resultUpload($ret);
}
$ret = $this->isUploadedFile(); //ตรวจสอบไฟล์
if($ret != 1){
return $this->resultUpload($ret);
}
$ret = $this->checkExtensionDir($dir); //ตรวจสอบ Dirtory
if($ret != 1){
return $this->resultUpload($ret);
}
$ret = $this->checkFileExists(); //ตรวจสอบไฟล์ซ้ำ
if($ret != 1){
return $this->resultUpload( $ret );
}
$ret = $this->move(); //ทำการ Upload
if( $ret != 1 ){
return $this->resultUpload($ret);
}
return $this->resultUpload("SUCCESS"); //ทำได้สำเร็จ
}
function resultUpload($flag){
switch($flag){
case "IS_UPLOADED_FILE_FAILURE" :
return "The file could not be uploaded
to the tmp directory of the web server.";
break;
case "DIRECTORY_FAILURE" :
return "The file could not be uploaded,
the directory is not writable.";
break;
case "EXTENSION_FAILURE" :
return "The file could not be uploaded,
this type of file is not accepted.";
break;
case "FILE_SIZE_FAILURE" :
return "The file could not be uploaded,
this file is too big.";
break;
case "FILE_EXISTS_FAILURE" :
return "The file could not be uploaded,
a file with the same name already exists.";
break;
case "MOVE_UPLOADED_FILE_FAILURE" :
return "The file could not be uploaded,
the file could not be copied to destination directory.";
break;
case "SUCCESS" :
return "Upload was successful!";
break;
}
}
}; // end class
?>
การเรียกใช้
<?php
include_once("Upload.class.php");
if( $_POST['submit'] != '' ){
$up = new MyUpload($_FILES['image']['name'],
$_FILES['image']['tmp_name'], $_FILES['image']['size'],
"thisname");
$result = $up->upload( "./" );
print $result;
}
print "<br><br> \n";
print "<form enctype='multipart/form-data'
method='post' action='". $PHP_SELF ."'> \n";
print "<input type='file' name='image'> \n";
print "<input type='submit' value='Upload' name='submit'> \n";
print "</form> \n";
?>