gpt4 book ai didi

php - 无法使用 cURL 将文件作为表单数据发送

转载 作者:行者123 更新时间:2023-12-03 00:11:33 26 4
gpt4 key购买 nike

我正在尝试通过命令行发送 cURL 请求。以下是我的要求

curl -i http://localhost/test/index.php 
-X POST
-F "name=file.png"
-F "content=@/var/www/html/test/file.png"

我遇到的问题是文件没有随请求一起发送。名称发送正常,但文件未发送。谁能看出我是否做了任何明显错误的事情?

我检查了文件的权限,因为我认为这可能是问题所在,但它们很好

后端是使用 PHP Slim 框架编写的,我正在执行以下操作 $app->request->post('content');

最佳答案

如果您希望能够使用 $app->request->post('content'); 访问文件的内容,您的curl请求必须使用<而不是@对于content场,

curl -i http://localhost/test/index.php -X POST -F "name=file.png" \
-F "content=</var/www/html/test/file.png"

来自curl的联机帮助页:

To force the 'content' part to be a file, prefix the file name with an @ sign. To just get the content part from a file, prefix the file name with the symbol <. The difference between @ and < is then that @ makes a file get attached in the post as a file upload, while the < makes a text field and just get the contents for that text field from a file.

通过使用 @ ,您将该字段标记为文件。从技术上讲,这会添加 Content-Disposition标题字段( RFC 2388 ,如果您有兴趣)。 PHP 检测到这一点并自动将该字段存储在 $_FILES 内而不是$_POST ,正如 Raphaël Malié 在评论中所说,这就是为什么您无法使用 $app->request->post 访问内容的原因。 .

您应该考虑改用 $_FILES不过,如果您想支持使用浏览器的 <input type=file> 上传,请在您的后端中进行元素和形式。那些总是设置 Content-Disposition header 。

关于php - 无法使用 cURL 将文件作为表单数据发送,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29979309/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com