PHP 文件编程综合案例-文件上传的实现(2)_PHP教程

编辑Tag赚U币
教程Tag:暂无Tag,欢迎添加,赚取U币!

推荐:解析在PHP中使用mysqli扩展库对mysql的操作
本篇文章是对在PHP中使用mysqli扩展库对mysql的操作进行了详细的分析介绍,需要的朋友参考下 1、在PHP中 使用mysqli扩展库对mysql 的dql操作 复制代码 代码如下: ?php header(Content-type: text/html;charset=utf-8); //mysqli操作mysql数据库(面向对象方式) //1、创建


<?php
    class Upload{
        public $upload_name; //上传文件名
        public $upload_tmp_path; //上传文件保存到服务器的temp路径
        public $file_size;
        public $file_type;
        public $file_save_path;
        function __construct(){
            $this->upload_name=$_FILES['myfile']['name'];
            $this->upload_tmp_path=$_FILES['myfile']['tmp_name'];
            $this->file_size=$_FILES['myfile']['size'];
            $this->file_type=$_FILES['myfile']['type'];
            $this->allow_file_type = array('jpeg','jpg','png','gif','bmp','doc','zip','rar','txt','wps','xlsx','ppt');
            $this->file_save_path=$_SERVER['DOCUMENT_ROOT']."/file/up/";
        }
        public function upload_file($username){
            //判断文件大小
            if($this->file_size>2*1024*1024){
                echo "<script type='text/javascript'>window.alert('文件不能大于2M')</script>";
                exit();
            }
            //获取文件类型
/*            if($this->file_type!="image/jpeg" && $this->file_type!="image/pjpeg"){
                echo "文件类型只能是 jpg 格式";
                exit();
            }
*/            //获取文件的扩展名
            $file_type=$this->getFileExt($this->upload_name);
            if(!in_array($file_type,$this->allow_file_type)){
                echo "上传文件类型格式错误";
                exit();
            }           
            //判断上传是否OK
            if(is_uploaded_file($this->upload_tmp_path)){

                //防止图片覆盖问题,为每个用户建立一个文件夹   
                $user_path=$this->file_save_path.$username;
                if(!file_exists($user_path)){
                    mkdir ($user_path);
                }
                //$move_to_file=$user_path."/".$_FILES['myfile']['name'];
                //防止用户上传用户名相同的问题
                //$file_true_name=$_FILES['myfile']['name'];
                $move_to_file=$user_path."/".time().rand(1,1000).substr($this->upload_name,strripos($this->upload_name,"."));
                //echo $upload_file.$move_to_file;
                //中文要转码
                if(move_uploaded_file($this->upload_tmp_path,iconv("utf-8","gb2312","$move_to_file"))){
                    echo $this->upload_name."上传成功";
                }else{
                    echo "上传失败";
                }
            }else{
                echo "上传失败";
            }
        }

        //获取文件的扩展名
        public function getFileExt($filename){
            $fileExt=pathinfo($filename);
            return $fileExt["extension"];
        }
    }
?>

分享:浅析PHP绘图技术
1、图片格式:目前网站开发常见的图片格式有gif,jpg/jpeg,png ..... 区别: gif 图片压缩率高,但是只能显示256色,可能造成颜色的丢失,可以显示动画 jpg/jpeg 图片压缩率高(有损压缩),可以用较小的文件来显示,网页上用得比较多 png 该格式综合了gif和jpg的优势,缺

共2页上一页12下一页
来源:模板无忧//所属分类:PHP教程/更新时间:2013-07-04
相关PHP教程