zoukankan      html  css  js  c++  java
  • 上传照片

    //上传照片
    function action_upload(){
    
        //获取上传文件的文件信息
        $photoname = $_FILES['photo']['name'];
        $type = $_FILES['photo']['type'];
        $tmp_name = $_FILES['photo']['tmp_name'];
        $size = $_FILES['photo']['size'];
        $error = $_FILES['photo']['error'];
        $maxSize = 20971520;
        $allowExt = array('gif', 'jpg', 'jpeg', 'png','image/jpeg');
        $destination ="static/upload/historyalbum/photo/".$photoname;
    
        //设置临时存放位置
        //move_uploaded_file($tmp_name,$destination);
    
        if($error==0){
            //判断照片大小
            if($size>$maxSize){
                exit('上传照片太大!:(');
            }
    
            if(!in_array($type,$allowExt)){
                exit('照片类型不对!:(');
            }
            //判断文件上传方式是否通过HTTP POST上传
            if(!is_uploaded_file($tmp_name)){
                exit('照片不是通过HTTP POST上传!:(');
            }
    
            //检测是否是真实的图片类型
            if(getimagesize($tmp_name) ==false){
                exit('照片非法!请选择正确的照片。:(');
            }
    
            //$photo_path = $destination;//文件目录为临时文件目录
            /*
                        if(!file_exists($photo_path)){
                            mkdir($photo_path,0777,true);
                            chmod($photo_path,0777);
                        }*/
    
            //确保文件名唯一,防止产生覆盖上传成功
            // $uniName = mad5(uniqid(microtime(true),true)).'.'.$tmp_name;
            if(@move_uploaded_file($tmp_name,$destination)){
                //echo '照片上传成功!:)';
    
                $photo = new Schoolhistoryphoto();
                $post['user_id'] = $_SESSION['id'];
                $post['ablum_id']=$_POST['type'];
                $post['name'] = $_POST['name'];
                $post['description'] = $_POST['description'];
                $post['photo_path'] = '/'.$destination;
                $post['create_at'] = date('Y-m-d H:i:s',time());
                $photo->fromArray($post);
                $photo->save();
    
                $this->request->redirect('schoolhistory/historyalbum');
            }else{
                //echo '照片上传失败!:(';
                $this->request->redirect('schoolhistory/historyalbum');
            }
    
        }else{
            //匹配错误信息
            switch($error){
                case 1:
                    echo '上传照片超过了PHP配置文件中upload_max_filesize选项的值:(';
                    break;
                case 2:
                    echo '超过了表单MAX_FILE_SIZE限制的大小:(';
                    break;
                case 3:
                    echo '照片部分被上传:(';
                    break;
                case 4:
                    echo '没有选择照片文件:(';
                    break;
                case 6:
                    echo '没有找到临时目录:(';
                    break;
                case 7:
                    echo '未知错误:(';
                    break;
                case 8:
                    echo '系统错误:(';
                    break;
            }
        }
    
    }
    我生活的地方,我为何要生活。
  • 相关阅读:
    Hadoop配置文件-mapred-site.xml
    Hadoop配置文件-core-site.xml
    Hadoop配置文件-hdfs-site.xml
    Hadoop学习-HDFS篇
    Android应用程序签名详解(转载)
    [Android]启用Service时Context.bindService()和Context.startService()的区别与联系(转载)
    Android模拟器代理设置(转载)
    如何在Android中利用Instrumentation来进行测试(转载)
    防止SQL注入的方法(转载)
    JavaScript常用语句 转载
  • 原文地址:https://www.cnblogs.com/hsd1727728211/p/5658565.html
Copyright © 2011-2022 走看看