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));
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 DIFY API Endpoint 问题。
  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突