文章详情

  • 游戏榜单
  • 软件榜单
关闭导航
热搜榜
热门下载
热门标签
php爱好者> php文档>(转)文件上传类

(转)文件上传类

时间:2009-07-19  来源:hkebao

<?php
/**
 * 作用:文件上传类
 * 时间:2006-8-20
 * 作者:欣然随风
 * 参数:
 * @file_input -- 表单上传框名称
 * @file_max -- 上传最大容量(KB)
 * @file_path -- 上传路径 例:d:/aa/bb/../../cc/ 或 d:/aa/bb/cc/,以"/"结束
 * @file_type -- 允许类型 例:$file_type = array('jpg','gif')
 * @file_name -- 上传后重命名称(不含扩展名)
 * @file_no -- 当目录不存在时是否生成
 * @file_del -- 开启后将在file_path下执行同名文件删除,删除类型包括file_type
 */
class class_upfile
{
    public $file_ream; // 返回真实扩展名

    public $err; // 返回报告

    public $off; // 返回状态


    function __construct($file_input,$file_max,$file_path,$file_type,$file_name,$file_no=false,$file_del=false)
    {
        $this->file_input = $file_input;
        $this->file_max = $file_max;
        $this->file_path = $file_path;
        $this->file_type = $file_type;
        $this->file_name = $file_name;
        $this->file_no = $file_no;
        $this->file_del = $file_del;

        $this->check();
        if($this->off)
        {
            if($_FILES[$this->file_input]['error'] == 0)
            {
                if($this->file_del)
                {
                    foreach($this->file_type AS $v)
                    {
                        $name = $this->file_path.$this->file_name.'.'.$v;
                        if(is_file($name)) unlink($name);
                    }
                }

                $newfile = $this->file_path.$this->file_name.$this->file_ream;
                $this->off = move_uploaded_file($_FILES[$this->file_input]['tmp_name'],$newfile);
                if($this->off)
                {
                    chmod($newfile,0777);
                    $this->err = '文件上传成功!';
                }else $this->err = '文件上传失败,请重试!';
            }
        }
    }

    /**
     * 方法:检测
     */
    function check()
    {
        // 是否为空

        if($_FILES[$this->file_input]['size'] == 0)
        {
            $this->err = '请选择上传文件!';
            return $this->off = false;
        }
        // 检查文件大小

        $file_size = $this->file_max * 1024; // 转为系统默认的字节

        if($_FILES[$this->file_input]['size'] > $file_size)
        {
            $this->err = '文件大小不得超过'.$this->file_max.'K!';
            return $this->off = false;
        }
        // 检查文件类型

        switch($_FILES[$this->file_input]['type'])
        {
            case 'image/pjpeg': $this->file_ream='jpg'; break;

            case 'image/gif': $this->file_ream='gif'; break;

            case 'text/plain': $this->file_ream='txt'; break;

            case 'application/msword': $this->file_ream='doc'; break;

            case 'application/x-shockwave-flash': $this->file_ream='swf'; break;

            case 'application/octet-stream': $this->file_ream='fla'; break;

            case 'application/octet-stream': $this->file_ream='rar'; break;

            case 'application/x-zip-compressed': $this->file_ream='zip'; break;

            case 'application/octet-stream': $this->file_ream='psd'; break;
        }
        if(!in_array($this->file_ream,$this->file_type))
        {
            $this->err = '此文件格式不支持!';
            return $this->off = false;
        }
        // 检查上传路径

        $this->file_path = $this->factpath($this->file_path);
        if(!is_dir($this->file_path))
        {
            if(!$this->file_no)
            {
                $this->err = '上传路径错误!';
                return $this->off = false;
            }else $this->_mkdirs($this->file_path);
        }

        $this->file_ream = '.'.$this->file_ream;
        return $this->off = true;
    }

    /**
     * 方法:换算实际路径
     */
    function factpath($path)
    {
        $adir = explode('/',$path);
        for($i=0;$i<count($adir);$i++)
        {
            $key = false;
            if($adir[$i] == '..') $key = $i;
            if($key !== false)
            {
                for($j=0;$j<count($adir);$j++)
                {
                    if($j==$key-1 || $j==$key) continue;
                    $newadir[] = $adir[$j];
                }
                $adir = $newadir;
                $newadir = false;
                $i=$i-2;
            }
        }
        return $path = implode('/',$adir);
    }

    /**
     * 方法:按指定路径生成目录
     */
    function _mkdirs($path)
    {
        $adir = explode('/',$path);
        $dirlist = '';
        $rootdir = array_shift($adir);
        if(!file_exists($rootdir))
            mkdir($rootdir);

        foreach($adir as $val)
        {
             $dirlist .= '/'.$val;
             $dirpath = $rootdir.$dirlist;
             if(!file_exists($dirpath))
             {
                 mkdir($dirpath);
                 chmod($dirpath,0777);
             }
        }
    }
}//class end


?>
相关阅读 更多 +
排行榜 更多 +
辰域智控app

辰域智控app

系统工具 下载
网医联盟app

网医联盟app

运动健身 下载
汇丰汇选App

汇丰汇选App

金融理财 下载