PHP cURL文件上载将信息附加到文件

PHP cURL文件上载将信息附加到文件,php,file-upload,curl,Php,File Upload,Curl,我将文件上传到远程服务器。守则: $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, ZDURL.$url); curl_setopt($ch, CURLOPT_USERPWD, ZDUSER."/token:".ZDAPIKEY); $params = array('file_name' => '@'.$temp_file); curl_setopt($ch, CURLOPT_HEADER, 0);

我将文件上传到远程服务器。守则:

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, ZDURL.$url);
    curl_setopt($ch, CURLOPT_USERPWD, ZDUSER."/token:".ZDAPIKEY);

    $params = array('file_name' => '@'.$temp_file);
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: text/plain"));
    curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

    curl_setopt($ch, CURLINFO_HEADER_OUT, 0);
    curl_setopt ($ch, CURLOPT_SSL_VERIFYHOST, 0);
    curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, 0);
    $result = curl_exec($ch);
    $error = curl_error($ch);
    curl_close($ch); 
但在上传文件的顶部会显示以下信息:

------------------------------cfbe90a606af
Content-Disposition: form-data; name="file_name"; filename="C:\Program Files\xampp\tmp\phpF576.tmp"
Content-Type: application/octet-stream

由于添加了此文本,图像的格式错误。听起来您发送数据的URL只需要请求正文中的一个文件,而不是表单提交。实现这一点的最佳方法是使用
CURLOPT_infle
。请尝试以下代码:

试试这个

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, ZDURL.$url);
curl_setopt($ch, CURLOPT_USERPWD, ZDUSER."/token:".ZDAPIKEY);

// You need to detect the correct content type for the file you are sending.
// Although based on the current problem you have, it looks like the server
// ignores this anyway
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: image/jpeg"));

// Send the raw file in the body with no other data
$fp = fopen($temp_file, 'r');
curl_setopt($ch, CURLOPT_INFILE, $fp);

curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLINFO_HEADER_OUT, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);

$result = curl_exec($ch);
$error = curl_error($ch);

curl_close($ch);
fclose($fp);

内容类型:text/plain
在这里没有任何意义,因为您谈论的是图像(通常在将数组传递给
CURLOPT_POSTFIELDS
时)。你想做什么,模拟表单提交,发布原始图像还是什么?发布原始图像。我用这个样本