首頁  >  文章  >  後端開發  >  PHP要怎麼實作微信圖片上傳到伺服器?

PHP要怎麼實作微信圖片上傳到伺服器?

零下一度
零下一度原創
2018-05-17 15:07:342221瀏覽

這篇文章主要介紹了PHP實作微信圖片上傳到伺服器的方法,涉及php基於curl操作圖片檔案的相關實作技巧,需要的朋友可以參考下方

本文實例講述了PHP實作微信圖片上傳到伺服器的方法。分享給大家參考,具體如下:

$pic_img=trim( $postObj->PicUrl);
if($type=="image"){
  $pic_url=save_file_to_sever($pic_img,$fromUsername);
}
   GetRootPath(){
  $sRealPath = realpath('./');
  $sSelfPath = $_SERVER['PHP_SELF'];
  $sSelfPath = substr($sSelfPath, 0, strrpos($sSelfPath, '/'));
  return substr($sRealPath, 0, strlen($sRealPath) - strlen($sSelfPath));
}
function save_file_to_sever($url,$fromid){
  $gen=GetRootPath();
  $time=time();
  $newfname =$gen."/weixin/wx_huodong/xiawucha/".$fromid.$time.'.jpg';
  $ch=curl_init();
  $timeout=5;
  curl_setopt($ch,CURLOPT_URL,$url);
  curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
  curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,$timeout);
  $img=curl_exec($ch);
  curl_close($ch);
  $size=strlen($img);
  //文件大小
  $fp2=@fopen($newfname,'a');
  fwrite($fp2,$img);
  fclose($fp2);
  return "www.xxx.com/weixin/wx_huodong/xiawucha/".$fromid.$time.'.jpg';
}

以上是PHP要怎麼實作微信圖片上傳到伺服器?的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn