php使用curl上傳文件


/**
* post請求上傳文件
* @param $url
* @param $post_data
* @return bool|string
*/
function curl_post($url,$post_data)
{

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_SAFE_UPLOAD, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
$output = curl_exec($ch);
curl_close($ch);
return $output;
}

$url是上傳地址
$post_data是上傳的參數
$post_data = [
  'name'  => 'xxxx',
  'file'  => new \CURLFile('絕對路徑'),    //文件中使用別的路徑沒有辦法上傳成功,只有絕對路徑成功,具體原因不明

];


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM