Upload POST data with files using cURL
P粉541796322
P粉541796322 2023-08-21 12:39:00
0
2
377
<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>
P粉541796322
P粉541796322

reply all(2)
P粉189606269

Capture user id as path variable (recommended):

curl -i -X POST -H "Content-Type: multipart/form-data" 
-F "data=@test.mp3" http://mysuperserver/media/1234/upload/

Capture the user id as part of the form:

curl -i -X POST -H "Content-Type: multipart/form-data" 
-F "data=@test.mp3;userid=1234" http://mysuperserver/media/upload/

or:

curl -i -X POST -H "Content-Type: multipart/form-data" 
-F "data=@test.mp3" -F "userid=1234" http://mysuperserver/media/upload/
P粉621033928

You need to use the -F option:
-F/--form <name=content> Specify HTTP multi-part POST data (H)

Please try the following:

curl \
  -F "userid=1" \
  -F "filecomment=这是一个图片文件" \
  -F "image=@/home/user1/Desktop/test.jpg" \
  localhost/uploader.php
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!