随手整理个代码上来,利用自带的curl扩展上传文件的实例:

<?php
$file     = curl_file_create('d:\1.txt');#文件绝对路径
$postData = array('extra_info' => '123456','file_contents'=> $file);
$postUrl  = "http://127.0.0.1/1.php";
$ch       = curl_init();
#curl_setopt($ch, CURLOPT_PROXY, "http://127.0.0.1:8888"); #设置代理方便自己分析发送过程
curl_setopt($ch, CURLOPT_URL, $postUrl);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
$r       = curl_exec($ch);
if(curl_errno($ch)){
    echo 'error: ' . curl_error($ch);
}else{
    echo $r;
}
curl_close($ch);

PHP接收端直接使用$_FILES即可。
以上内容参考自:https://stackoverflow.com/questions/15200632/how-to-upload-file-using-curl-with-php#answer-15200804

Related Posts: PHP curl上传文件示例 :

avatar