First study curl the same domain upload image and then study Curl cross-domain upload, first the same domain upload code summarized as follows:
index.php
<?PHP//image Upload$url= "http://192.168.1.230/curl/receive.php";$post _data=Array ( "foo" = "Bar",//The local file address to upload, starting with php5.5.0 @, and using the new Curlfile () mode instead"Upload" =NewCurlfile ("D:/workspace/xlyy/curl/img/1.png"),//The path must be the absolute path of the disk);$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,$post _data);$output= Curl_exec ($ch); Curl_close ($ch);Echo' <pre> ';Print_r(Json_decode ($output,true));Echo' </pre> ';
receive.php
<?PHPif($_files){ $filename=$_files[' Upload '] [' Name ']; $tmpname=$_files[' Upload '] [' Tmp_name ']; if(Move_uploaded_file($tmpname,dirname(__file__).‘ /upload/'.$filename)){ EchoJson_encode (' Upload succeeded ',Json_unescaped_unicode); }Else{ EchoJson_encode (' Upload failed ',Json_unescaped_unicode); } } ?>
Note: php5.5.0 begins to discard the @ method, using the new Curlfile () mode instead
Curl upload image (same domain upload)