上传照片

//上传照片
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;
        }
    }

}

 

posted @ 2016-07-10 21:05  mokal同学  阅读(345)  评论(0编辑  收藏  举报