zoukankan      html  css  js  c++  java
  • php <5.5 curl post and file 请求

    /**
         * 跨域post请求(适配文件上传)
         * @param $url 上传地址
         * @param $post_data 请求数据
         */
         protected function sendPost ($url,$post_data)
        {
            /*$url = "http://10.0.0.87/fileAccept.php";
            $post_data = array(
                "dir" => "wechat",
                //要上传的本地文件地址
                "upload" => '@/alidata/www/15c56d316166c1fccfb036a9337d0caeChrysanthemum.jpg'
            );*/
            $ch = curl_init();
            curl_setopt($ch , CURLOPT_URL , $url);
            curl_setopt($ch , CURLOPT_RETURNTRANSFER, 1);
            curl_setopt($ch , CURLOPT_POST, 1);
            curl_setopt($ch , CURLOPT_POSTFIELDS, $post_data);
            $output = curl_exec($ch);
            curl_close($ch);
            return $output;
        }
    

      接收:

    <?php 
    
    /**
    * 跨域文件接收
    */
    class fileAccept
    {
    	protected $dis = '/alidata/www/cdnappota/'; 
    	protected $ur = 'http://域名/';
    	
    	// Function to get the client IP address
    	function get_client_ip() 
    	{
    	    $ipaddress = '';
    	    if (getenv('HTTP_CLIENT_IP'))
    	        $ipaddress = getenv('HTTP_CLIENT_IP');
    	    else if(getenv('HTTP_X_FORWARDED_FOR'))
    	        $ipaddress = getenv('HTTP_X_FORWARDED_FOR');
    	    else if(getenv('HTTP_X_FORWARDED'))
    	        $ipaddress = getenv('HTTP_X_FORWARDED');
    	    else if(getenv('HTTP_FORWARDED_FOR'))
    	        $ipaddress = getenv('HTTP_FORWARDED_FOR');
    	    else if(getenv('HTTP_FORWARDED'))
    	        $ipaddress = getenv('HTTP_FORWARDED');
    	    else if(getenv('REMOTE_ADDR'))
    	        $ipaddress = getenv('REMOTE_ADDR');
    	    else
    	        $ipaddress = 'UNKNOWN';
    	    return $ipaddress;
    	}
    
    	function uploadFile($files,$dir='images')
    	{
    
    		if ($files) {
    		 	$time = time();
    	        $newname = md5(uniqid().$time);
    	        if (!file_exists($this->dis.$dir.'/')) {
    	        	mkdir($this->dis.$dir.'');
    	        }
    	       	$res =  move_uploaded_file($files['tmp_name'],$this->dis.$dir.'/'.$newname.$files['name']);
    			if ($res) {
    				echo json_encode(['errno'=>0,'data'=>[$this->ur.$dir.'/'.$newname.$files['name']]]);
    			}else{
    				echo json_encode(['errno'=>1]);
    			}
    			
    		}
    	}
    
    }
    
    $files = $_FILES['upload'];
    $dir = $_POST['dir'];
    
    $acc = new fileAccept();
    $ip = $acc->get_client_ip();
    $whiteIp = ['10.0.0.117'];//ip白名单
    if (in_array($ip,$whiteIp)) {
    	echo $acc->uploadFile($files,$dir);
    }
    

      

  • 相关阅读:
    GitHub加速 ,GitHub下载速度太慢,国内github访问加速,github速度慢解决办法
    vscode 配置(格式化代码)
    微信小程序生成二维码(完美解决因链接过长报错问题)
    git 本地和仓库同步
    【转】JavaScript 判断iPhone X Series机型的方法
    【转】Js apply方法详解
    【转】JS生成指定范围内的随机数(支持随机小数)
    移动端浮动靠边按钮
    判断访问设备类型(android、ios、微信、pc)
    函数返回值
  • 原文地址:https://www.cnblogs.com/leescre/p/8004207.html
Copyright © 2011-2022 走看看