首页 > php代码 > php 常用的文件上传类-可多文件上传

php 常用的文件上传类-可多文件上传

<?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]; // 获取文件后缀;
    $tmp = 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="http: //www.phprm.com"> ";
        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;
        }
    }
}


教程地址:http://www.phprm.com/code/b547a2b7eeee646e0b0835eb758f7c1a.html

欢迎转载!但请带上文章地址^^

标签:none

发表留言