作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
直到今天,我一直在使用以下脚本,没有任何问题。我换了linux的发行版,不知道有没有关系
$full = realpath($newPath."/".$image);
$data = array('name' => $name, 'pic' => '@'.$full.";type=image/jpeg");
$ch = curl_init();
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: multipart/form-data"));
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_COOKIEFILE, 'cookie.txt');
curl_setopt($ch, CURLOPT_COOKIEJAR, 'cookie.txt');
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
$contents = curl_exec($ch);
我通过 php 的表单发布信息,但我无法提交图像,因为它没有被检测为图像,它被检测为“application/octet-stream”
如何将内容类型指定为图像并同时提交文本?
PD我查看了我之前提出的类似问题,我只是找到了一个具有确切目的的问题并且没有任何回复。我不是要下载任何图片,我是想用几个文本字段填写一个表格,我应该通过同一个表格上传一张图片
最佳答案
好的,所以我找到了问题所在。您需要使用 CurlFile() 类,而不是 '@' 。 $file_name 示例。
文档:http://php.net/manual/en/class.curlfile.php
代替
$postfields['file_contents'] = '@' . $filename;
你会想要这样做
$postfields['file_contents'] = new CurlFile($filename);
您可以查看文档以在构造函数的另一端指定 mime 类型和名称,但这是它的基本要点。
祝你好运!
关于php - 使用 curl 和 php 脚本上传图像和文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38922677/
我有以下正则表达式 /[a-zA-Z0-9_-]/ 当字符串只包含从 a 到z 大小写、数字、_ 和 -。 我的代码有什么问题? 能否请您向我提供一个简短的解释和有关如何修复它的代码示例? //var
我是一名优秀的程序员,十分优秀!