This article mainly introduces the PHP implementation of the image upload to the server method, involving PHP based on the curl operation of the image file implementation skills, the need for friends can refer to the next
The example of this article describes how PHP implements the image upload to the server. Share to everyone for your reference, as follows:
$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); File size $fp 2= @fopen ($newfname, ' a '); Fwrite ($fp 2, $img); Fclose ($fp 2); Return "www.xxx.com/weixin/wx_huodong/xiawucha/". $fromid. $time. JPG ';}