dsxsou8465 2014-02-19 17:30
浏览 52
已采纳

PHP Curl发布没有标题的文件

I'm not much of a PHP person, but trying to port a cli CURL call to PHP ... originally I had:

curl -i -H "Content-Type: text/csv" -X POST --data-binary @$filepath $destination

I've tried to turn this into:

<?php
$data['Filedata'] = "@".$filepath;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $destination.'/_');
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: text/csv"));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$output = curl_exec($ch); 
curl_close($ch);
?>

The problem is that this results in an extra header being prepended to the destination, containing ------------------------------6614b0cf8aae Content-Disposition: form-data; name=Filedata; filename=$filepath Content-Type: application/octet-stream.

I assumed this was because of the way I used $data['Filedata'], so I tried replacing the CURLOPT_POSTFIELDS line with the below, but that just winds up posting the path string rather than the file itself.

curl_setopt($ch, CURLOPT_POSTFIELDS, "@".$filepath);

Anyone mind pointing out what I'm missing? Like I said, not a PHP person!

  • 写回答

1条回答 默认 最新

  • douhuantui6259 2014-02-19 17:39
    关注

    Just load the file content directly and it will do. Currently you are doing key/value pared POST data which is different from your original curl commandline.

    curl_setopt($ch, CURLOPT_POSTFIELDS, file_get_contents($filepath));
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 个人网站被恶意大量访问,怎么办
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大