php - 使用 curl 和 php 脚本上传图像和文本

标签 php image curl file-upload

直到今天,我一直在使用以下脚本,没有任何问题。我换了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/

相关文章:

php - 为什么我的 PHP 上传脚本无法在 jquery Mobile 上运行?

Java swing gui 更改 jlabel 的背景并使其重置

curl - 为什么 curl 不能下载 ffmpeg-2.7.ta​​r.bz2?

ssl - 如何 curl 不安全的 HTTPS

php - 使用 jquery ajax 将值从表单的文本字段传递到 php

php - 使用另一个 symfony2 项目作为供应商

ruby-on-rails - 在 Rails 中将图像存储为模型

javascript - 在 Javascript 中从外部源导入 JSON feed

php - 如何在 BuzzBrowser post 调用中将文件数据作为 post 参数发送

java - 如何将图像从android发送到servlet然后存储