if (!function_exists('curl_file_create')) {
function curl_file_create($filename, $mimetype = '', $postname = '') {
return "@$filename;filename="
. ($postname ?: basename($filename))
. ($mimetype ? ";type=$mimetype" : '');
}
}
$ch = curl_init();
$filename = basename($_FILES['myFile_cover']['name']);
$minetype = $_FILES['myFile_cover']['type'];
$curl_file = curl_file_create($filename,$minetype);
$postData = array(
"profile-picture" => $curl_file
);
curl_setopt($ch, CURLOPT_URL,$api_edit_avatar);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: multipart/form-data'));
$res = curl_exec($ch);
$error_no = curl_errno($ch);
$info = curl_getinfo($ch);
$err_msg = '';
if ($error_no) {
$err_msg = curl_error($ch);
echo $err_msg;
} else {
print_r($res);
dump($info);
}
curl_close($ch);
這是我找到其中一個方式
但卻還是行不通
甚至還報錯: couldn't open file 'xxxxxxx.jpg'
我的php版本是7 不知道跟這個有沒有關係
要如何透過curl丟圖片到遠端?!