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

📄 uploader.php

📁 一个很漂亮的相册系统
💻 PHP
字号:
<?php 
/* 
是upload的class*/ 
require("config.php"); 
class upload{ 
    var $error_msg = ''; 

    function upload($input_field_name){ 
        $this->input_field_name = $input_field_name; 
    } 


    function set_accepted_mime_types($accepted_mime_types){ 
        $this->accepted_mime_types = $accepted_mime_types; 
    } 


    function set_max_file_size($max_size){ 
        $this->max_file_size = $max_size; 
    } 


    function set_max_image_size($width, $height){ 
        $this->max_image_width = $width; 
        $this->max_image_height = $height; 
    } 


    function draw_simple_form($title = "上传", $action = ''){ 
        if (isset($this->max_file_size)){ 
            $maxlenght = " maxlength=\"{$this->max_file_size}\""; 
        }else{ 
            $maxlenght = ''; 
        } 

        if (isset($this->accepted_mime_types)){ 
            $accept = ' accept="' . implode(',', $this->accepted_mime_types) . '"'; 
        }else{ 
            $accepted = ''; 
        } 

        echo "<form enctype=\"multipart/form-data\" action=\"{$action}\" method=\"post\">"; 
        echo $title . ": <input name=\"{$this->input_field_name}\" type=\"file\"$accept$maxlenght>"; 
        echo "<input type=\"submit\" value=\"上传图片\" title=\"不用刷新的哦\">"; 
        echo "</form>"; 
    } 


    function draw_form($title = "上传", $action = ''){ 
        if (isset($this->max_file_size)){ 
            $maxlenght = " maxlength=\"{$this->max_file_size}\""; 
        }else{ 
            $maxlenght = ''; 
        } 

        if (isset($this->accepted_mime_types)){ 
            $accept = ' accept="' . implode(',', $this->accepted_mime_types) . '"'; 
        }else{ 
            $accepted = ''; 
        } 

        echo "<form enctype=\"multipart/form-data\" action=\"{$action}\" method=\"post\">"; 
        echo $title . ": <br>"; 
        echo "<input name=\"{$this->input_field_name}\" type=\"file\"$accept$maxlenght size=\"30\">"; 
        echo "<br><input type=\"submit\" value=\"上传图片\" title=\"不用刷新的哦\">"; 
        echo "</form>"; 

        if (isset($this->accepted_mime_types)){ 
            echo "仅允许上传的图片类型 " . implode(', ', $this->accepted_mime_types) . "<br>如果不知道MIME类型对应的格式的话,请自己搜索或者访问<a href=http://www.hivemail.com/mime.types.phps target=_blank>http://www.hivemail.com/mime.types.phps</a>"; 
        } 
    } 


    function security_check(){ 
        if (is_uploaded_file($_FILES[$this->input_field_name][tmp_name])){ 
            $this->file = $_FILES[$this->input_field_name]; 
        }else{ 
            $this->error_msg = "上传文件"; 
            return false; 
        } 

        if(isset($this->max_file_size) && ($this->file["size"] > $this->max_file_size)){ 
            $this->error_msg .= "晕,上传的图片文件太大了!允许上传的图片最大为 " . $this->max_file_size . " b ( " . round($this->max_file_size / 1024, 2) . "Kb)"; 
            return false; 
        } 

        if(ereg("image", $this->file["type"])){ 
            $image_size = getimagesize($this->file["tmp_name"]); 

            if(isset($this->max_image_width) && isset($this->max_image_height) && 
                    ($image_size[0] > $this->max_image_width) || ($image_size[1] > $this->max_image_height)){ 
                $this->error_msg .= "晕,上传的图片宽/高超出限制了 。 允许上传的原始图片是" . $this->max_image_width . " × " . $this->max_image_height . " px"; 
                return false; 
            } 
        } 


        if(isset($this->accepted_mime_types) && !in_array($this->file["type"], $this->accepted_mime_types)){ 
            $this->error_msg = "不允许上传该类型文件!<br>\n"; 
            $this->error_msg .= "该文件类型是: {$this->file["type"]}<br>\n"; 
            $this->error_msg .= "允许上传的类型是: " . implode(', ', $this->accepted_mime_types); 
            return false; 
        } 

        return true; 
    } 

    function move($destination_folder, $overwrite = false){ 
        if ($this->security_check() == false){ 
            return false; 
        } 

        $filename = $this->file['tmp_name']; 
        $destination = $destination_folder . $this->file['name']; 
        if (file_exists($destination) && $overwrite != true){ 
            $this->error_msg = "同名文件已经存在了"; 
            return false; 
        }elseif (move_uploaded_file ($filename, $destination)){ 
            return true; 
        }else{ 
            $this->error_msg = "移动文件时出错!"; 
            return false; 
        } 
    } 

    function read(){ 
        if ($this->security_check() == false){ 
            return false; 
        } 

        $filename = $this->file['tmp_name']; 
        $fd = fopen ($filename, "rb"); 
        $contents = fread ($fd, filesize ($filename)); 
        fclose ($fd); 

        return $contents; 
    } 
} 

?> 

⌨️ 快捷键说明

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