⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 fileupload.php

📁 FileUpload 文件上传类
💻 PHP
📖 第 1 页 / 共 2 页
字号:

    function set_allowed_types($types){
        $this->allowed_types = explode('|', $types);
    }

    function set_image_properties($path = ''){
        if ( ! $this->is_image()){
            return;
        }

        if (function_exists('getimagesize')) {
            if (FALSE !== ($D = @getimagesize($path)))
            {
                $types = array(1 => 'gif', 2 => 'jpeg', 3 => 'png');

                $this->image_width      = $D['0'];
                $this->image_height     = $D['1'];
                $this->image_type       = ( ! isset($types[$D['2']])) ? 'unknown' : $types[$D['2']];
                $this->image_size_str   = $D['3'];  // string containing height and width
            }
        }
    }


    function is_image() {
        // IE will sometimes return odd mime-types during upload, so here we just standardize all
        // jpegs or pngs to the same file type.

        $png_mimes  = array('image/x-png');
        $jpeg_mimes = array('image/jpg', 'image/jpe', 'image/jpeg', 'image/pjpeg');

        if (in_array($this->file_type, $png_mimes))
        {
            $this->file_type = 'image/png';
        }

        if (in_array($this->file_type, $jpeg_mimes))
        {
            $this->file_type = 'image/jpeg';
        }

        $img_mimes = array(
                            'image/gif',
                            'image/jpeg',
                            'image/png',
                           );

        return (in_array($this->file_type, $img_mimes, TRUE)) ? TRUE : FALSE;
    }

    function is_allowed_file_type(){
        if (count($this->allowed_types) == 0 OR ! is_array($this->allowed_types)){
            $this->set_error('upload_no_file_allowed_types');
            return FALSE;
        }

//      echo '上传文件的MIME:'.$this->file_type.'<br />';  // 文件的MIME
//		echo '允许的MIME:<br />';

        foreach ($this->allowed_types as $val){
            $mime = $this->mimes_types(strtolower($val));
// 			print_r($mime);echo '<br />';    // 允许的mime
            if (is_array($mime)){
                if (in_array($this->file_type, $mime, TRUE)){
                    return TRUE;
                }
            }
            else{
                if ($mime == $this->file_type){
                    return TRUE;
                }
            }
        }

        return FALSE;
    }

    function is_allowed_filesize()
    {
        if ($this->max_size != 0  AND  $this->file_size > $this->max_size){
            return FALSE;
        }
        else{
            return TRUE;
        }
    }

    function is_allowed_image_dimensions()
    {
        if ( ! $this->is_image()){
            return TRUE;
        }

        if (function_exists('getimagesize')){
            $D = @getimagesize($this->file_temp);

            if ($this->max_width > 0 AND $D['0'] > $this->max_width){
                return FALSE;
            }

            if ($this->max_height > 0 AND $D['1'] > $this->max_height){
                return FALSE;
            }

            return TRUE;
        }

        return TRUE;
    }


    function validate_upload_path() {
        if ($this->upload_path == ''){
            $this->set_error('upload_no_filepath');
            return FALSE;
        }

        if (function_exists('realpath') AND @realpath($this->upload_path) !== FALSE) {
            $this->upload_path = str_replace("\\", "/", realpath($this->upload_path));
        }

        if ( ! @is_dir($this->upload_path)) {
            $this->set_error('upload_no_filepath');
            return FALSE;
        }

        if ( ! $this->is_really_writable($this->upload_path)) {
            $this->set_error('upload_not_writable');
            return FALSE;
        }

        $this->upload_path = preg_replace("/(.+?)\/*$/", "\\1/",  $this->upload_path);
        return TRUE;
    }


    function get_extension($filename){
        return substr_count(substr($filename,0,-1), '.') == 0 ? '' : substr(strrchr($filename, '.'),1);
    }

    function clean_file_name($filename){
        $bad = array(
				"<!--", "-->", "'", "<", ">", '"', '&', '$', '=', ';', '?', '/', "%20","%22","%3c",
				"%253c","%3e", "%0e", "%28", "%29", "%2528", "%26", "%24", "%3f", "%3b", "%3d"
				);
        $filename = str_replace($bad, '', $filename);
        return stripslashes($filename);
    }

    function limit_filename_length($filename, $length){
        if (strlen($filename) < $length){
            return $filename;
        }

        $ext = '';
        if (strpos($filename, '.') !== FALSE){
            $parts      = explode('.', $filename);
            $ext        = '.'.array_pop($parts);
            $filename   = implode('.', $parts);
        }

        return substr($filename, 0, ($length - strlen($ext))).$ext;
    }

    function set_error($msg)    {
        echo $msg."<br />";
    }


    function display_errors($open = '<p>', $close = '</p>')
    {
        $str = '';
        foreach ($this->error_msg as $val){
            $str .= $open.$val.$close;
        }

        return $str;
    }


    function mimes_types($mime){
        if (count($this->mimes) == 0) {
            if (@require_once('MIMES.php')) {
                $this->mimes = $gMimes;
            }
        }
        return ( ! isset($this->mimes[$mime])) ? FALSE : $this->mimes[$mime];
    }

    function _prep_filename($filename){
        if (strpos($filename, '.') === FALSE){
            return $filename;
        }
        $parts      = explode('.', $filename);
        $ext        = array_pop($parts);
        $filename   = array_shift($parts);

        foreach ($parts as $part){
            if ($this->mimes_types(strtolower($part)) === FALSE){
                $filename .= '.'.$part.'_';
            }
            else{
                $filename .= '.'.$part;
            }
        }

        $filename .= '.'.$ext;
        return $filename;
    }

    function is_really_writable($file){
        if (DIRECTORY_SEPARATOR == '/' AND @ini_get("safe_mode") == FALSE){
            return is_writable($file);
        }

        if (is_dir($file)){
            $file = rtrim($file, '/').'/'.md5(rand(1,100));

            if (($fp = @fopen($file, 'a+b')) === FALSE){
                return FALSE;
            }

            fclose($fp);
            @chmod($file, '0777');
            @unlink($file);
            return TRUE;
        }
        elseif (($fp = @fopen($file, 'ab')) === FALSE){
            return FALSE;
        }

        fclose($fp);
        return TRUE;
    }

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -