使用curl来上传POST数据和文件 [英] Using curl to upload POST data with files
问题描述
我想使用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:
userid = 12345
filecomment =这是一个图片文件
userid = 12345 filecomment = This is an image file
HTTP文件上传:
文件位置= /home/user1/Desktop/test.jpg
文件的表单名称= 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< name = content>指定HTTP多部分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屋!