设为首页 收藏本站
查看: 1300|回复: 0

[经验分享] 文件上传类 PHP 个人自定义版本

[复制链接]
累计签到:1 天
连续签到:1 天
发表于 2015-8-23 11:59:06 | 显示全部楼层 |阅读模式
DSC0000.gif DSC0001.gif View Code


<?php
/**
* 上传接口
* @author BraveCheng
*   
*/
abstract class Upload {
    public $upName; // 文件名称
    public $upMaxSize = 8142; // 文件上传大小
    public $upPath; // 上传路径
    public $upTypes = array (
            'jpg',
            'gif',
            'png'
    ); // 文件类型
    public $time; // 文件上传时间或者自定义上传目录
    public $destFolder; // 目标文件夹
    public $upError; // 错误代号
    public $sucssInfo = array (); // 成功日志
    abstract protected function fileUpload();
    abstract protected function errMsg(); // 错误日志
}
/**
* 文件上传类
*
* @author BraveCheng
*        
*/
class CustomUpload extends Upload {
    protected $upFullPathName; // 上传之后的完整路径名称
    protected $upFullName; // 上传之后的完整名称
    protected $fileField; // 上传文件名称空间,如$_FILES['field']['name'].
    protected $chkFileExt; // 上传文件名称后缀
    protected $chkFileSize; // 上传文件实际大小
    protected $chkFileName; // 上传文件名称
    /**
     *
     * @param 自定义路径 $custPath            
     * @param 自定义名称 $custName            
     * @param 自定义类型 $custTypes            
     * @param 自定义最大尺寸 $custMaxSize            
     */
    function __construct($custDir, $custPath, $custName, $custTypes = array(), $custMaxSize, $fileField) {
        $this->destFolder = $custDir; // 自定义上传的文件路径
        $this->upPath = $custPath;
        $this->upName = $this->setName ( $custName );
        $this->upTypes = $custTypes;
        $this->upMaxSize = $custMaxSize;
        $this->fileField = ( string ) $fileField;
        $this->time = time (); // 初始化主要用来统一文件名称和目录
    }
    /**
     * 重命名上传文件,支持中文名
     *
     * @param string $custName            
     * @return string
     */
    private function setName($custName) {
        return ! empty ( $custName ) ? iconv ( "utf-8", "gbk", $custName ) : date ( 'YmdHis', $this->time ) . mt_rand ( 10, 99 );
        /*
         * if ($custName == '') { // 如果未设置文件名,则生成一个随机文件名 $name = date (
         * 'YmdHis',$this->time ) . "_" . mt_rand ( 10, 99 ) . '.' . $this->ext;
         * // 判断文件是否存在,不允许重复文件 if (file_exists ( $this->savePath . $name )) {
         * $name = setSavename ( $saveName ); } } else { $name = $saveName; }
         * $this->saveName = $name; }
         */
    }
    private function setPath() {
        return (preg_match ( '/\/$/', $this->upPath )) ? $this->upPath : $this->upPath . '/';
    }
    /**
     * 创建目录
     *
     * @param string $baseDir            
     * @param string $destDir            
     */
    private function mkDirs($baseDir, $destDir) {
        $dirs = $baseDir;
        ! is_dir ( $dirs ) && @mkdir ( $dirs, 0777 ); // 原来如果前面的假是正的。后面的语句就执行
        if (! empty ( $destDir )) {
            $destDirs = explode ( '/', $destDir );
            foreach ( $destDirs as $finalDir ) {
                ! empty ( $finalDir ) && $dirs .= $finalDir . '/';
                ! is_dir ( $dirs ) && @mkdir ( $dirs, 0777 );
            }
        } else {
            $dirs .= date ( 'Ymd', $this->time ) . '/';
            ! is_dir ( $dirs ) && @mkdir ( $dirs, 0777 );
        }
        return $dirs;
    }
    /**
     * 获得后缀函数
     *
     * @param string $fileName            
     * @return mixed
     */
    private function getFileExt($filename) {
        $extend = pathinfo ( $filename );
        $this->chkFileExt = $extend ['extension'];
    }
    /**
     * 检测文件后缀函数
     *
     * @return boolean
     */
    private function checkFileExt() {
        if (in_array ( $this->chkFileExt, $this->upTypes )) { // 此处程序有bug
            return TRUE;
        } else {
            $this->upError = 1;
            return FALSE;
        }
    }
    /**
     * 检测最大尺寸
     *
     * @return boolean
     */
    private function checkFileMaxSize() {
        if ($this->chkFileSize > $this->upMaxSize) {
            $this->upError = 2;
            return FALSE;
        }
        return TRUE;
    }
    /*
     * (non-PHPdoc) @see Upload::fileUpload()
     */
    public function fileUpload() {
        // 单文件、多文件上传
        $keys = array_keys ( $_FILES [$this->fileField] ['name'] );
        foreach ( $keys as $key ) {
            if (! $_FILES [$this->fileField] ['name'] [$key])
                continue;
            $sysError = $_FILES [$this->fileField] ['error'] [$key];
            switch ($sysError) {
                case 1 :
                    $this->upError = 3;
                    break;
                case 2 :
                    $this->upError = 4;
                    break;
                case 3 :
                    $this->upError = 5;
                    break;
                case 4 :
                    $this->upError = 6;
                    break;
                case 5 :
                    $this->upError = 7;
                    break;
            }
            $this->chkFileName = iconv ( "utf-8", "gbk", $_FILES [$this->fileField] ['name'] [$key] ); // 循环中的文件名称
            $this->chkFileSize = $_FILES [$this->fileField] ['size'] [$key]; // 循环中的文件大小
            $this->getFileExt ( $this->chkFileName );
            // 文件类型检测
            if (! $this->checkFileExt ()) {
                return $this->errMsg ();
                exit ();
            }
            // 超过大小
            if (! $this->checkFileMaxSize ()) {
                return $this->errMsg ();
                exit ();
            }
            if ($sysError == 0 && is_uploaded_file ( $_FILES [$this->fileField] ['tmp_name'] [$key] )) {
                // 组装文件名称
                /*
                 * $upFullPathName = $this->upName . $key . '.' .
                 * $this->chkFileExt; // 不允许重复 if (file_exists ( $upFullPathName
                 * )) { $this->upFullName = $upFullPathName; }
                 */
                $this->upFullName = $this->upName . $key . '.' . $this->chkFileExt;
                $this->upFullPathName = $this->mkDirs ( $this->destFolder, $this->setPath () ) . $this->upFullName;
                if (move_uploaded_file ( $_FILES [$this->fileField] ['tmp_name'] [$key], $this->upFullPathName )) {
                    $this->sucssInfo ['name'] = $this->upFullPathName;
                    $this->sucssInfo ['size'] = $this->chkFileSize;
                    $this->sucssInfo ['info'] = '文件<font color=red>' . $this->upFullName . '</font>上传成功!';
                }
            }
        }
        return $this->sucssInfo;
    }
    /*
     * (non-PHPdoc) @see Upload::errMsg()
     */
    public function errMsg() {
        $errMsg = array (
                0 => '文件上传成功!',
                1 => '上传文件<font color=red>' . $this->chkFileName . '</font>类型错误,只支持上传<font color=red>' . implode ( ',', $this->upTypes ) . '</font>等文件类型!',
                2 => '上传文件<font color=red>' . $this->chkFileName . '</font>太大,最大支持<font color=red>' . ceil ( $this->upMaxSize / 1024 ) . '</font>kb的文件',
                3 => '上传文件<font color=red>' . $this->chkFileName . '</font>超过了 php.ini 中 upload_max_filesize 选项限制的值。',
                4 => '上传文件<font color=red>' . $this->chkFileName . '</font>大小超过了 HTML 表单中 MAX_FILE_SIZE 选项指定的值!',
                5 => '文件<font color=red>' . $this->chkFileName . '</font>只有部分被上传!',
                6 => '没有文件被上传。',
                7 => '文件上传失败!'
        );
        if ($this->upError == 0)
            return false;
        else
            return $errMsg [$this->upError];
    }
}
  发布下自己定义的上传类!环境新人和高手拍砖!
  



# custom define page
if( access_has_global_level( config_get( 'manage_site_threshold' ) ) ) {
$t_menu_options[] = '<a href="' . helper_mantis_url( 'custorm_define_list_page.php">' ) . lang_get( 'custorm_list_link' ) . '</a>';
$t_menu_options[] = '<a href="' . helper_mantis_url( 'ot_application_page.php">' ) . lang_get( 'ot_link' ) . '</a>';
$t_menu_options[] = '<a href="' . helper_mantis_url( 'payment_application_page.php">' ) . lang_get( 'payment_link' ) . '</a>';
}
# contact list
if( !current_user_is_anonymous() ) {
$t_menu_options[] = '<a href="' . helper_mantis_url( 'view_contact_list.php">' ) . lang_get( 'contact_list_link' ) . '</a>';
}

  

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-102945-1-1.html 上篇帖子: PHP 5.5 新特性 下篇帖子: PHP排序算法的复习和总结
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表