使用 cURL 上传文件时,文件会被修改为 header 信息

When uploading a file with cURL the file gets modified with header information

使用 cURL 上传文件时,我发现远程端的文件正在修改,header 信息被转储到文件中。我怀疑这是我的 header 信息中的内容,但我迷路了。我几乎在尝试任何事情。

这是我正在使用的代码。

$fLocation = realpath('file.csv');  
$finfo = finfo_open(FILEINFO_MIME_TYPE);  
$fType = finfo_file($finfo, $fLocation);  
finfo_close($finfo);      
$cFile = curl_file_create($fLocation);  
$post = array('file'=> $cFile);  
$ch = curl_init();  
curl_setopt($ch, CURLOPT_URL, $url);  
curl_setopt($ch, CURLOPT_POST,1);  
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);  
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);  
curl_setopt($ch, CURLOPT_USERPWD, $username.":".$password);  
curl_setopt($ch, CURLOPT_HTTPHEADER, "Content-Type: application/octet-stream");  
$result = curl_exec($ch);  

我为 header 尝试了一些不同的 Content-Types,从 application/binary 到 multipart/form-data,甚至根本没有发送 content-type在 header 年代。我怀疑我需要在 header 中添加更多内容。

无论如何,我要发送的文件是 .csv,但我想最后发送一个 .xlsx 文件(我正在使用 .csv 进行测试)。当我从远程服务器获取文件并在文本编辑器中打开它时,我看到文件的开头和结尾添加了信息,文件的原始数据介于两者之间。这是添加到顶部的内容。

=--------------------------a0fe4530540c659b  
Content-Disposition: form-data; name="file"; filename="/home/username/website/path/file.csv"  
Content-Type: application/octet-stream 

还有,底部

--------------------------a0fe4530540c659b--

基本上,当我的所有 .xlsx 文件到达另一端后都已损坏时,我发现了这一点。我将要发送的内容更改为纯文本文件 (CSV),那时我才意识到发生了什么。

这里有更多有趣的信息。这实际上是我通过他们的 API 向 ZenDesk 发送了一些东西,我显然是在 PHP 中使用 cURL 这样做的。当我收到来自 Zendesk 的 JSON 回复时,它的一部分看起来像这样...

[file_name] => file.csv  
[content_url] => https://org.zendesk.com/attachments/token/jdjdjdjdjdjhdjdjdj/?name=file.csv  
[content_type] => multipart/form-data  
[size] => 5615  

有趣的是,我上传的内容是 multipart/form-data。即使我在 header.

中更改 Content-Type 也是如此

我当然错过了一些愚蠢而简单的东西。

您不需要像 Content-Type: application/octet-stream 或其他设置那样设置 headers 来上传文件。只需要使用 curl_file_create 方法发送您的文件。

您的代码必须是这样的:

if (function_exists('curl_file_create')) { // for php 5.5+
    $File_Address = curl_file_create($file_name_with_full_path);
} else {
    $File_Address = '@' . realpath($file_name_with_full_path);
}

$post = array('file_name'=> $File_Address);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST,1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_USERPWD, $username.":".$password);

$result = curl_exec($ch);

众所周知,这与headers有关。这是最终为我解决问题的代码。

$ch = curl_init();
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_MAXREDIRS, 10 );
curl_setopt($ch, CURLOPT_URL, $json_url);
curl_setopt($ch, CURLOPT_USERPWD, $username.":".$password);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-type: application/binary'));
curl_setopt($ch, CURLOPT_POST, true);
$file = fopen($fLocation, 'r');
$size = filesize($fLocation);
$fildata = fread($file,$size);
curl_setopt($ch, CURLOPT_POSTFIELDS, $fildata);
curl_setopt($ch, CURLOPT_INFILE, $file);
curl_setopt($ch, CURLOPT_INFILESIZE, $size);
curl_setopt($ch, CURLOPT_USERAGENT, "MozillaXYZ/1.0");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
curl_setopt($ch, CURLOPT_VERBOSE, true);
$result = curl_exec($ch);