<?php
class My_Lib_upfile{
var $upfile, $upfile_name, $upfile_size;
# $upfile 臨時檔案名稱 $_FILES['tmp_name'] ,$upfile_name 檔案名稱 $_FILES['name'] ,$upfile_size 檔案大小$_FILES['size'];
var $new_upfile_name; # 上傳後的檔案名稱 ;
var $fleth, $fileExtent; # 副檔名(類型) ;
var $f1, $f2, $f3; # 檔案儲存路徑(多級) upfiles/2008-01/08/;
var $filename; # 檔案(帶路徑) ;
var $filepath; #相對路徑用來刪除檔案;
var $maxSize, $File_type; # 允許上傳檔案的大小 允許上傳檔案的類型 ;
var $BuildFile,$newFile,$File_width,$File_height,$rate;
function upfileclass($upfile,$upfile_name,$upfile_size){
$this->upfile = $upfile;
$this->upfile_name = $upfile_name;
$this->upfile_size = $upfile_size;
$this->new_upfile_name = $this->CreateNewFilename($this->upfile_name);
$this->f1 = "public/upload/images";
$this->f2 = $this->f1."/".date('Y')."-".date('m');
$this->f3 = $this->f2."/".date('d');
$this->filename = $this->f3 . "/" . $this->new_upfile_name;
$this->maxSize = 5000*1024; # 檔案大小 5000KB
$this->File_type = "gif/jpg/jpeg/png/bmp"; # 允許上傳的檔案類型
}
# 建立新檔案名稱 (原檔案名稱)
function CreateNewFilename($file_name){
$this->fleth = explode(".",$file_name);
$this->fileExtent = $this->fleth[(int)count($this->fleth)-1]; # 擷取檔案尾碼;
$tmps教程tr = date('Ymd').rand(0,time()) . "." .$this->fileExtent; # 建立新檔案名稱;
return $tmpstr;
}
# 檢測檔案類型是否正確
function chk_fileExtent(){
$iwTrue = 0;
$fle = explode("/",$this->File_type);
for($i=0; $i < count($fle); $i++){
if($this->fileExtent == $fle[$i]){
$iwTrue = (int) $iwTrue + 1;
}
}
if( $iwTrue == 0 ){
$this->msg("檔案不符合 ".$this->File_type." 格式!");
}
}
# 提示錯誤資訊並終止操作
function msg($Error){
echo "<script language="javascript教程"> ";
echo " alert('".$Error."'); ";
echo " window.history.back(); ";
echo "</script> ";
die();
}
# 儲存檔案
function savefile(){
$this->chk_fileExtent();
$this->chk_fileSize();
$this->CreateFolder( "./".$this->f1 );
$this->CreateFolder( "./".$this->f2 );
$this->CreateFolder( "./".$this->f3 );
return $this->chk_savefile();
}
# 檢測上傳結果是否成功
function chk_savefile(){
$copymsg = copy($this->upfile,"./".$this->filename);
if( $copymsg ){
return $this->filename;
}
else{
$this->msg("檔案上傳失敗! 請重新上傳! ");
}
}
# 建立檔案夾
function CreateFolder($foldername){
if( !is_dir($foldername) ){
mkdir($foldername,0777);
}
}
# 檢測檔案大小
function chk_fileSize(){
if( $this->upfile_size > $this->maxSize ){
$this->msg("目標檔案不能大於". $this->maxSize/1024 ." KB");
}
}
# 刪除檔案($filePath 檔案相對路徑)
function Deletefile($filePath){
if( !is_file($filePath) ){
return false;
}
else{
$ending = @unlink($filePath);
return $ending;
}
}
}
?>