欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

php+curl 发送图片处理代码分享

程序员文章站 2022-05-06 08:39:13
//上传页面代码 $url = "http://192.168.1.100/upload.php?lang=cn"; #可以get传相应参数...

//上传页面代码

  $url = "http://192.168.1.100/upload.php?lang=cn";  #可以get传相应参数
  $file = $path.'/'. $icon; //要上传的文件
  $fields['f'] = '@'.$file;
  $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, $fields );
  curl_exec( $ch );
  if ($error = curl_error($ch) ) {
    die($error);
  }
  curl_close($ch); 

    //接收图片资源

  date_default_timezone_set('asia/shanghai'); //设置时区
  $ip = '';
  if(getenv('http_client_ip') && strcasecmp(getenv('http_client_ip'), 'unknown')){
        $ip = getenv('http_client_ip');
    }elseif(getenv('http_x_forwarded_for') && strcasecmp(getenv('http_x_forwarded_for'), 'unknown')){
        $ip = getenv('http_x_forwarded_for');
    }elseif(getenv('remote_addr') && strcasecmp(getenv('remote_addr'), 'unknown')){
        $ip = getenv('remote_addr');
    }elseif(isset($_server['remote_addr']) && $_server['remote_addr'] && strcasecmp($_server['remote_addr'], 'unknown')){
        $ip = $_server['remote_addr'];
    }
    preg_match("/[\d\.]{7,15}/", $ip, $ipmatches);
    $ip = $ipmatches[0] ? $ipmatches[0] : 'unknown';

    //驗證請求ip,设置白名单
    $date = date("[y-m-d h:i:s]");
    $allowip = array('192.168.1.100');
    if(!in_array($ip,$allowip)){
        $ipval = "{$date}{$ip}\n";
        file_put_contents('upload_ip.log', $ipval, file_appen);
        die(-1);
    }

    //接收get传参
    $lang = isset($_request['lang']) ? $_request['lang'] : 'cn'; 
    $uploaddir = "/data/cdncache/res/gifts/{$lang}/";
    $uploadfile = $uploaddir . $_files['f']['name'];
    if (move_uploaded_file($_files['f']['tmp_name'], $uploadfile)) #保存图片成功
    {
        $type = $_files['f']['type'];
        $tmp_name = $_files['f']['tmp_name'];
        $error = $_files['f']['error'];
        $size = $_files['f']['size'];
        $log_txt = ' name:' . $_files['f']['name'] . ' type:' . $type . ' tmp_name:' . $tmp_name . ' error:' . $error . ' size:' . $size . " hello " . ' lang:' . $_request['lang'];
        $value = "{$date}{$log_txt}\n";
        @file_put_contents('upload_ok.log', $value, file_append);
        exit();
    } else {
        $type = $_files['f']['type'];
        $tmp_name = $_files['f']['tmp_name'];
        $error = $_files['f']['error'];
        $size = $_files['f']['size'];
        $log_txt = ' name:' . $_files['f']['name'] . ' type:' . $type . ' tmp_name:' . $tmp_name . ' error:' . $error . ' size:' . $size . " hello " . ' lang:' . $_request['lang'];
        $value = "{$date}{$log_txt}\n";
        @file_put_contents('upload_ng.log', $value, file_append);
        exit();
  }

以上所述就是本文的全部内容了,希望大家能够喜欢。