Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using cURL to upload POST data with files

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 parameters:

userid = 12345 filecomment = This is an image file

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

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 

The problem I am getting is as follows:

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

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

How do I adjust my cURL commands accordingly ?

like image 589
thotheolh Avatar asked Oct 01 '12 05:10

thotheolh


People also ask

Can curl upload files?

Uploading files using CURL is pretty straightforward once you've installed it. Several protocols allow CURL file upload including: FILE, FTP, FTPS, HTTP, HTTPS, IMAP, IMAPS, SCP, SFTP, SMB, SMBS, SMTP, SMTPS, and TFTP. Each of these protocols works with CURL differently for uploading data.

Can you post with curl?

Users can send data with the POST request using the -d flag. The following POST request sends a user and a pass field along with their corresponding values. POSTing with curl's -d option will include a default header that looks like: Content-Type: application/x-www-form-urlencoded .


1 Answers

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

Try this:

curl \   -F "userid=1" \   -F "filecomment=This is an image file" \   -F "image=@/home/user1/Desktop/test.jpg" \   localhost/uploader.php 
like image 68
jimp Avatar answered Sep 28 '22 23:09

jimp