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);
    }
    

      

  • 相关阅读:
    [转]C++ Operator Overloading Guidelines
    SICP学习笔记(2.2.1)
    .net中模拟键盘和鼠标操作
    javaScript系列 [17]运算符
    javaScript系列 [24]Math
    javaScript系列 [19]string
    javaScript系列 [22]引用类型
    javaScript系列 [12]Canvas绘图(曲线)
    javaScript系列 [15]Canvas绘图(压缩)
    javaScript系列 [21]Array
  • 原文地址:https://www.cnblogs.com/leescre/p/8004207.html
Copyright © 2011-2022 走看看