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

这篇文章主要介绍了php+curl 发送图片处理代码分享的方法的相关资料,需要的朋友可以参考下,上传页面代码:

  1. $url = "http://192.168.1.100/upload.php?; #可以get传相应参数
  2. $file = $path.'/'. $Icon; //要上传的文件
  3. $fields['f'] = '@'.$file;
  4. $ch = curl_init();
  5. curl_setopt($ch, CURLOPT_URL, $url );
  6. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  7. curl_setopt($ch, CURLOPT_POST, 1 );
  8. curl_setopt($ch, CURLOPT_POSTFIELDS, $fields );
  9. curl_exec( $ch );
  10. if ($error = curl_error($ch) ) {
  11. die($error);
  12. }
  13. curl_close($ch);

接收图片资源:

  1. date_default_timezone_set('Asia/Shanghai'); //设置时区
  2. $ip = '';
  3. if(getenv('HTTP_CLIENT_IP') && strcasecmp(getenv('HTTP_CLIENT_IP'), 'unknown')){
  4. $ip = getenv('HTTP_CLIENT_IP');
  5. }elseif(getenv('HTTP_X_FORWARDED_FOR') && strcasecmp(getenv('HTTP_X_FORWARDED_FOR'), 'unknown')){
  6. $ip = getenv('HTTP_X_FORWARDED_FOR');
  7. }elseif(getenv('REMOTE_ADDR') && strcasecmp(getenv('REMOTE_ADDR'), 'unknown')){
  8. $ip = getenv('REMOTE_ADDR');
  9. }elseif(isset($_SERVER['REMOTE_ADDR']) && $_SERVER['REMOTE_ADDR'] && strcasecmp($_SERVER['REMOTE_ADDR'], 'unknown')){
  10. $ip = $_SERVER['REMOTE_ADDR'];
  11. }
  12. preg_match("/[\d\.]{7,15}/", $ip, $ipmatches);
  13. $ip = $ipmatches[0] ? $ipmatches[0] : 'unknown';
  14. //驗證請求IP,设置白名单
  15. $date = date("[Y-m-d H:i:s]");
  16. $allowip = array('192.168.1.100');
  17. if(!in_array($ip,$allowip)){
  18. $ipVal = "{$date}{$ip}\n";
  19. file_put_contents('upload_ip.log', $ipVal, FILE_APPEN);
  20. die(-1);
  21. }
  22. //接收get传参
  23. $lang = isset($_REQUEST['lang']) ? $_REQUEST['lang'] : 'CN';
  24. $uploaddir = "/data/cdncache/res/gifts/{$lang}/";
  25. $uploadfile = $uploaddir . $_FILES['f']['name'];
  26. if (move_uploaded_file($_FILES['f']['tmp_name'], $uploadfile)) #保存图片成功
  27. {
  28. $type = $_FILES['f']['type'];
  29. $tmp_name = $_FILES['f']['tmp_name'];
  30. $error = $_FILES['f']['error'];
  31. $size = $_FILES['f']['size'];
  32. $log_txt = ' name:' . $_FILES['f']['name'] . ' type:' . $type . ' tmp_name:' . $tmp_name . ' error:' . $error . ' size:' . $size . " hello " . ' lang:' . $_REQUEST['lang'];
  33. $value = "{$date}{$log_txt}\n";
  34. @file_put_contents('upload_ok.log', $value, FILE_APPEND);
  35. exit();
  36. } else {
  37. $type = $_FILES['f']['type'];
  38. $tmp_name = $_FILES['f']['tmp_name'];
  39. $error = $_FILES['f']['error'];
  40. $size = $_FILES['f']['size'];
  41. $log_txt = ' name:' . $_FILES['f']['name'] . ' type:' . $type . ' tmp_name:' . $tmp_name . ' error:' . $error . ' size:' . $size . " hello " . ' lang:' . $_REQUEST['lang'];
  42. $value = "{$date}{$log_txt}\n";
  43. @file_put_contents('upload_ng.log', $value, FILE_APPEND);
  44. exit();
  45. }

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