使用 cURL 上传带有文件的 POST 数据 [英] Using cURL to upload POST data with files

查看:61
本文介绍了使用 cURL 上传带有文件的 POST 数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用 cURL 不仅可以在 HTTP POST 中发送数据参数,还可以上传具有特定表单名称的文件.我该怎么做?

I would like to use cURL to not only send data parameters in HTTP POST but to also upload files with specific form name. How should I go about doing that ?

HTTP Post 参数:

HTTP Post parameters:

用户 ID = 12345filecomment = 这是一个图像文件

userid = 12345 filecomment = This is an image file

HTTP 文件上传:文件位置 =/home/user1/Desktop/test.jpgfile = image的表单名(对应PHP端的$_FILES['image'])

HTTP File upload: File location = /home/user1/Desktop/test.jpg Form name for file = image (correspond to the $_FILES['image'] at the PHP side)

我认为 cURL 命令的一部分如下:

I figured part of the cURL command as follows:

curl -d "userid=1&filecomment=This is an image file" --data-binary @"/home/user1/Desktop/test.jpg" localhost/uploader.php

我遇到的问题如下:

Notice: Undefined index: image in /var/www/uploader.php

问题是我使用 $_FILES['image'] 来获取 PHP 脚本中的文件.

The problem is I am using $_FILES['image'] to pick up files in the PHP script.

如何相应地调整我的 cURL 命令?

How do I adjust my cURL commands accordingly ?

推荐答案

你需要使用-F选项:
-F/--form 指定HTTP multipart POST 数据(H)

试试这个:

curl 
  -F "userid=1" 
  -F "filecomment=This is an image file" 
  -F "image=@/home/user1/Desktop/test.jpg" 
  localhost/uploader.php

这篇关于使用 cURL 上传带有文件的 POST 数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆