php 常用的檔案上傳類-可多檔案上傳

來源:互聯網
上載者:User

<?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;
    }
 }

 

}
?>

聯繫我們

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