标签 php 下的文章

Thinkphp自适应上传多个或单个文件

/**
     * 上传图片
     * @return mixed
     */
	protected function uploadImg()
    {
        $upload = new \Think\Upload();  // 实例化上传类
        $upload->maxSize   =     3145728 ;  // 设置附件上传大小
        $upload->exts      =     array('jpg', 'gif', 'png', 'jpeg');    // 设置附件上传类型
        $upload->rootPath  =     './Uploads/';  // 设置附件上传根目录
        $upload->savePath  =     'images/';    //设置附件上传(子)目录
        // 上传文件
        $info   =   $upload->upload();
        if(!$info) {// 上传错误提示错误信息

            $msg['status'] = 2;
            $msg['msg'] = $upload->getError();
        }else{// 上传成功
            $msg['status'] = 1;
            $msg['result'] = self::getImg($info,$upload->rootPath);
            $msg['msg'] = '上传成功!';
        }
        return $msg;
    }

    /**
     * @param $data 数据元
     * @param $file 文件根路径
     * @return array|string
     */
    static function getImg ( $data , $file ) {

	    $filename = substr($file,1);

        if (count($data) === 1) {
            $newarr = current($data);
            $info = $filename . $newarr['savepath'].$newarr['savename'];
            return $info;
        }else{
            $arr = array();
            foreach ($data as $key => $value){
                $arr[$key] = $filename . $value['savepath'].$value['savename'];
            }
            return $arr;
        }
    }

 

php通过经纬度计算两点之间的距离并排序

现在很多项目都会用到通过获取用户地理信息计算最近的商家,最近也在折腾这方面,先上代码:

    /**
    *  @desc 根据两点间的经纬度计算距离
    *  @param float $lat 纬度值
    *  @param float $lng 经度值
    */
     static function getDistance($lat1, $lng1, $lat2, $lng2)
     {
         $earthRadius = 6367000; //approximate radius of earth in meters

         /*
           Convert these degrees to radians
           to work with the formula
         */

         $lat1 = ($lat1 * pi() ) / 180;
         $lng1 = ($lng1 * pi() ) / 180;

         $lat2 = ($lat2 * pi() ) / 180;
         $lng2 = ($lng2 * pi() ) / 180;

         /*
           Using the
           Haversine formula

           http://en.wikipedia.org/wiki/Haversine_formula

           calculate the distance
         */

         $calcLongitude = $lng2 - $lng1;
         $calcLatitude = $lat2 - $lat1;
         $stepOne = pow(sin($calcLatitude / 2), 2) + cos($lat1) * cos($lat2) * pow(sin($calcLongitude / 2), 2);  $stepTwo = 2 * asin(min(1, sqrt($stepOne)));
         $calculatedDistance = $earthRadius * $stepTwo;

         return round($calculatedDistance) / 1000;   //除以1000 得到的结果就是公里
     }

得到距离后,我们再对这个数组进行排序。

/**
     * [suiSort 数组排序]
     * @param  [type] $data [源数据]
     * @return [type]       [description]
     */
    static function suiSort ($data)
    {
        $sort = array(  
            'direction' => 'SORT_ASC', //排序顺序标志 SORT_DESC 降序;SORT_ASC 升序  
            'field'     => 'juli',       //排序字段  
        ); 

        $arrSort = array();  
        foreach($data AS $uniqid => $row){  
            foreach($row AS $key=>$value){  
                $arrSort[$key][$uniqid] = $value;  
            }  
        }  

        if($sort['direction']){  
            array_multisort($arrSort[$sort['field']], constant($sort['direction']), $data);  
        }  
        return $data;
    }