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

📄 fileupload.php

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

class FileUpload {

    var $max_size       = 0;
    var $max_width      = 0;
    var $max_height     = 0;
    var $max_filename   = 0;
    var $allowed_types  = "";
    var $file_temp      = "";
    var $file_name      = "";
    var $orig_name      = "";
    var $file_type      = "";
    var $file_size      = "";
    var $file_ext       = "";
    var $upload_path    = "";
    var $overwrite      = FALSE;
    var $encrypt_name   = FALSE;
    var $is_image       = FALSE;
    var $image_width    = '';
    var $image_height   = '';
    var $image_type     = '';
    var $image_size_str = '';
    var $error_msg      = array();
    var $mimes          = array();
    var $remove_spaces  = TRUE;

    function FileUpload($config = array()){
        if (count($config) > 0){
            $defaults = array(
                                'max_size'          => 0,
                                'max_width'         => 0,
                                'max_height'        => 0,
                                'max_filename'      => 0,
                                'allowed_types'     => "",
                                'file_temp'         => "",
                                'file_name'         => "",
                                'orig_name'         => "",
                                'file_type'         => "",
                                'file_size'         => "",
                                'file_ext'          => "",
                                'upload_path'       => "",
                                'overwrite'         => FALSE,
                                'encrypt_name'      => FALSE,
                                'is_image'          => FALSE,
                                'image_width'       => '',
                                'image_height'      => '',
                                'image_type'        => '',
                                'image_size_str'    => '',
                                'error_msg'         => array(),
                                'mimes'             => array(),
                                'remove_spaces'     => TRUE
                            );
            foreach ($defaults as $key => $val) {
                if (isset($config[$key])) {
                    $method = 'set_'.$key;
                    if (method_exists($this, $method)) {
                        $this->$method($config[$key]);
                    }
                    else {
                        $this->$key = $config[$key];
                    }
                }
                else{
                    $this->$key = $val;
                }
            }
        }
    }


    function do_upload($field = 'userfile')
    {
        if ( ! isset($_FILES[$field])){
            $this->set_error('upload_no_file_selected');
            return FALSE;
        }

        if ( ! $this->validate_upload_path()){
            return FALSE;
        }

        if ( ! is_uploaded_file($_FILES[$field]['tmp_name'])){
            $error = ( ! isset($_FILES[$field]['error'])) ? 4 : $_FILES[$field]['error'];
            switch($error){
                case 1: // UPLOAD_ERR_INI_SIZE
                    $this->set_error('upload_file_exceeds_limit');
                    break;
                case 2: // UPLOAD_ERR_FORM_SIZE
                    $this->set_error('upload_file_exceeds_form_limit');
                    break;
                case 3: // UPLOAD_ERR_PARTIAL
                   $this->set_error('upload_file_partial');
                    break;
                case 4: // UPLOAD_ERR_NO_FILE
                   $this->set_error('upload_no_file_selected');
                    break;
                case 6: // UPLOAD_ERR_NO_TMP_DIR
                    $this->set_error('upload_no_temp_directory');
                    break;
                case 7: // UPLOAD_ERR_CANT_WRITE
                    $this->set_error('upload_unable_to_write_file');
                    break;
                case 8: // UPLOAD_ERR_EXTENSION
                    $this->set_error('upload_stopped_by_extension');
                    break;
                default :   $this->set_error('upload_no_file_selected');
                    break;
            }

            return FALSE;
        }

        $this->file_temp = $_FILES[$field]['tmp_name'];
        $this->file_name = $this->_prep_filename($_FILES[$field]['name']);
        $this->file_size = $_FILES[$field]['size'];
        $this->file_type = preg_replace("/^(.+?);.*$/", "\\1", $_FILES[$field]['type']);
        $this->file_type = strtolower($this->file_type);
        $this->file_ext  = $this->get_extension($_FILES[$field]['name']);

        if ($this->file_size > 0) {
            $this->file_size = round($this->file_size/1024, 2);  // kb
        }

        if ( ! $this->is_allowed_file_type()) {
            $this->set_error('upload_invalid_file_type');
            return FALSE;
        }

        if ( ! $this->is_allowed_filesize()) {
            $this->set_error('upload_invalid_filesize');
            return FALSE;
        }

        if ( ! $this->is_allowed_image_dimensions()) {
            $this->set_error('upload_invalid_image_dimensions');
            return FALSE;
        }

        $this->file_name = $this->clean_file_name($this->file_name);

        if ($this->max_filename > 0){
            $this->file_name = $this->limit_filename_length($this->file_name, $this->max_filename);
        }

        if ($this->remove_spaces == TRUE){
            $this->file_name = preg_replace("/\s+/", "_", $this->file_name);
        }

        $this->orig_name = $this->file_name;
        if ($this->overwrite == FALSE){
            $this->file_name = $this->set_filename($this->upload_path, $this->file_name);

            if ($this->file_name === FALSE){
                return FALSE;
            }
        }
//		echo $this->upload_path.$this->file_name;exit;
        if ( ! @copy($this->file_temp, $this->upload_path.$this->file_name)){
            if ( ! @move_uploaded_file($this->file_temp, $this->upload_path.$this->file_name)){
                 $this->set_error('upload_destination_error');
                 return FALSE;
            }
        }
echo $this->file_name;exit;
        $this->set_image_properties($this->upload_path.$this->file_name);

        return TRUE;
    }

    function data(){
        return array (
                        'file_name'         => $this->file_name,
                        'file_type'         => $this->file_type,
                        'file_path'         => $this->upload_path,
                        'full_path'         => $this->upload_path.$this->file_name,
                        'raw_name'          => str_replace($this->file_ext, '', $this->file_name),
                        'orig_name'         => $this->orig_name,
                        'file_ext'          => $this->file_ext,
                        'file_size'         => $this->file_size,
                        'is_image'          => $this->is_image(),
                        'image_width'       => $this->image_width,
                        'image_height'      => $this->image_height,
                        'image_type'        => $this->image_type,
                        'image_size_str'    => $this->image_size_str,
                    );
    }

    function set_upload_path($path){
        $this->upload_path = rtrim($path, '/').'/';
    }

    function set_filename($path, $filename)
    {
        if ($this->encrypt_name == TRUE){
            mt_srand();
            $filename = md5(uniqid(mt_rand())).'.'.$this->file_ext;
        }

        if ( ! file_exists($path.$filename)){
            return $filename;
        }

        $filename = str_replace($this->file_ext, '', $filename);

        $new_filename = '';
        for ($i = 1; $i < 100; $i++){
            if ( ! file_exists($path.$filename.$i.$this->file_ext)){
                $new_filename = $filename.$i.$this->file_ext;
                break;
            }
        }

        if ($new_filename == ''){
            $this->set_error('upload_bad_filename');
            return FALSE;
        }
        else{
            return $new_filename;
        }
    }

    function set_max_filesize($n){
        $this->max_size = ((int) $n < 0) ? 0: (int) $n;
    }

    function set_max_filename($n){
        $this->max_filename = ((int) $n < 0) ? 0: (int) $n;
    }

    function set_max_width($n){
        $this->max_width = ((int) $n < 0) ? 0: (int) $n;
    }

    function set_max_height($n){
        $this->max_height = ((int) $n < 0) ? 0: (int) $n;
    }

⌨️ 快捷键说明

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