服务器之家

服务器之家 > 正文

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

时间:2020-11-02 17:22     来源/作者:PHP教程网

//上传页面代码

?
1
2
3
4
5
6
7
8
9
10
11
12
13
$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);

    //接收图片资源

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
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();
}

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

标签:

相关文章

热门资讯

2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全
2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全 2019-12-26
Intellij idea2020永久破解,亲测可用!!!
Intellij idea2020永久破解,亲测可用!!! 2020-07-29
歪歪漫画vip账号共享2020_yy漫画免费账号密码共享
歪歪漫画vip账号共享2020_yy漫画免费账号密码共享 2020-04-07
电视剧《琉璃》全集在线观看 琉璃美人煞1-59集免费观看地址
电视剧《琉璃》全集在线观看 琉璃美人煞1-59集免费观看地址 2020-08-12
最新idea2020注册码永久激活(激活到2100年)
最新idea2020注册码永久激活(激活到2100年) 2020-07-29
返回顶部