Upload POST data with files using cURL
P粉541796322
2023-08-21 12:39:00
<p>I want to use cURL not only to send data parameters in HTTP POST but also to upload a file with a specific form name. How can I do this? </p>
<p>HTTP POST parameters: </p>
<p>userid = 12345
filecomment = This is a picture file</p>
<p>HTTP file upload:
File location = /home/user1/Desktop/test.jpg
The form name of the file = image (corresponding to $_FILES['image'] on the PHP side)</p>
<p>I have found part of the cURL command as follows: </p>
<pre class="brush:php;toolbar:false;">curl -d "userid=1&filecomment=This is a picture file" --data-binary @"/home/user1/Desktop/test.jpg " localhost/uploader.php</pre>
<p>The problem I encountered is as follows:</p>
<pre class="brush:php;toolbar:false;">Note: Undefined index in /var/www/uploader.php: image</pre>
<p>The problem is that I am using $_FILES['image'] in the PHP script to receive the file. </p>
<p>How should I adjust my cURL commands accordingly? </p>
Capture user id as path variable (recommended):
Capture the user id as part of the form:
or:
You need to use the
-F
option:-F/--form <name=content> Specify HTTP multi-part POST data (H)
Please try the following: