Home  >  Q&A  >  body text

Upload POST data with files using cURL

<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粉541796322P粉541796322394 days ago387

reply all(2)I'll reply

  • P粉189606269

    P粉1896062692023-08-22 11:11:35

    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/

    reply
    0
  • P粉621033928

    P粉6210339282023-08-22 00:18:22

    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

    reply
    0
  • Cancelreply