【问题标题】:Upload a file with php curl使用 php curl 上传文件
【发布时间】:2013-03-15 09:45:59
【问题描述】:

发送错误的 mime 类型。文件类型为“video/mp4”,但“application / octet-stream”作为发送。我想发送“视频/mp4”。 卷曲选项($file = 真实路径)

$post = array('videoupload'=>'1','input_1'=>"@$file");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS,$post);
curl_setopt($ch, CURLOPT_INFILESIZE,(string)filesize($file));
curl_setopt($ch, CURLOPT_INFILE,fopen($file,'r'));

Http 调试器输出

------------------94a50e65d9fe 内容处置:表单数据;名称="视频上传"

1 ------------------94a50e65d9fe 内容处置:表单数据;名称=“输入_1”;文件名="1.mp4" 内容类型: 应用程序/八位字节流

我试过了

$post = array('videoupload'=>'1','input_1'=>"@$file;type=video/mp4");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS,$post);
curl_setopt($ch, CURLOPT_INFILESIZE,(string)filesize($file));
curl_setopt($ch, CURLOPT_INFILE,fopen($file,'r'));

curl_error 输出:

创建表单数据失败

【问题讨论】:

  • 你不想CURLOPT_HTTPHEADER吗?见:stackoverflow.com/questions/356705/…
  • 标头正确(Content-Type: multipart/form-data")。远程服务器检查文件mime-type($_FILES['input_1']["type"])。
  • 您可能需要更新问题以使其更清晰。无论如何,您永远都不想信任$_FILES[..]['type'] - 如果您需要这样做,请尝试使用finfo 之类的东西。
  • Http 调试器(Google Chrome):Content-type:multipart/form-data; tny.cz/d4a8fba5Http Debugger(Curl): Content-type:multipart/form-data; tny.cz/acd09926
  • 这篇文章会帮助其他人吗?也许OP可以更新问题并更好地解释解决方案?我什至不知道我是否理解所提出的问题。

标签: php libcurl


【解决方案1】:

尝试像这样在 curl_setopt 中添加 CURLOPT_HTTPHEADER

curl_setopt($ch, CURLOPT_HTTPHEADER,array('Content-Type: video/mp4'));

【讨论】:

  • header 是正确的(Content-Type: multipart/form-data")。因为不仅视频。数据可用。
  • 好的,但是php.net/manual/en/curl.installation.php 说,In order to enable this module on a Windows environment, libeay32.dll and ssleay32.dll must be present in your PATH. You don't need libcurl.dll from the cURL site.。我们真的需要 curl 在 windows 上运行 php-curl 吗?
  • 但是,如果标题内容类型在 Windows 上具有不同的行为,这是可能的。
  • 我解决了这个问题。 stackoverflow.com/questions/15622814/…
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-04-22
  • 1970-01-01
  • 2012-11-24
相关资源
最近更新 更多