Posting a File with Curl [PHP Code]

To post (or upload) a file with Curl, use the -d or -F command-line options and start the data with the @ symbol followed by the file name. To upload multiple files, repeat the -F option several times. Curl will automatically provide the Content-Type header based on the file extension, but you can indicate a custom Content-Type header using the -H command-line option. The file will be sent using the HTTP POST method. You can specify a different HTTP method using the -X command line parameter. For security reasons, ReqBin Online Curl Client does not support sending files from its disk. Click Run to execute the Curl Post File example online and see the results. The PHP code was automatically generated for the Curl POST File example.
Posting a File with Curl [PHP Code] Run
curl -d @data.json 
     https://reqbin.com/echo/post/json
Updated: Viewed: 66653 times
PHP code for Curl POST File example

PHP code for Curl POST File Example

This PHP code snippet was generated automatically for the Curl POST File example.
<< Back to the Curl POST File example

What is Curl?

Curl (stands Client URL) is a command-line tool that runs on Windows, Linux, and macOS platforms, designed for transferring data from a server or to a server using many popular network protocols, including HTTP, HTTPS, and FTP. Curl has built-in support for SSL, proxies, certificate validation, HTTP cookies, and user authentication.

What is HTTP POST?

HTTP POST is the most widely used HTTP protocol method for sending files and data to a server. The POST method asks the web server to accept and process the data contained in the body of the POST message. The POST method is commonly used for uploading files, submitting HTML forms, and CRUD operations when creating or updating a resource on the server. POST requests can change the server's state and are not idempotent, unlike GET and HEAD request methods.

How to send HTTP POST requests using Curl?

You can send HTTP POST requests using Curl by explicitly specifying the required HTTP method with the -X POST command line parameter. When submitting data using the -d command-line option, Curl automatically selects the HTTP POST method. If you want Curl to use a different HTTP method, such as HTTP PUT, you can specify this with the -X PUT command-line option.

Curl send POST request example
curl -X PUT -d '{"id": 1}' https://reqbin.com/echo/post/json

How to send a file using Curl?

o upload a file, use the -d command-line option and begin data with the @ symbol. If you start the data with @, the rest should be the file's name from which Curl will read the data and send it to the server.

Curl post file example
curl -d @path/to/data.json https://reqbin.com/echo/post/json

Curl will use the file extension to send the correct MIME data type. For example, if you send a JSON file using the command line -d @data.json parameters, Curl will automatically send the Content-Type: application/json HTTP header with your request. If you want to send data with a different type, you can use the -H command-line option and specify your content type.

Send file with a custom Content-Type header example
curl https://reqbin.com/echo/post/json
   -d @path/to/data.json 
   -H "Content-Type: application/javascript"

Curl post file syntax

The general form of the Curl command for posting a file is as follows:

Syntax POST file with Curl
curl -d @filename [URL]

Where:
  • -d: @filename: file to send to server

How to upload files with multipart/form-data content type using Curl?

Browsers use the multipart/form-data content type when you upload multiple files to the server via web forms. To submit data with multipart/form-data content type with Curl, use the -F (or --form) command line parameter and precede the parameter value with an @ sign.

Curl submit file with multipart/form-data
curl -F logo=@filename [URL]

What is the difference between the -d and -F options?

The -d command-line option will force Curl to submit data to the server using the application/x-www-form-urlencoded format. At the same time, the -F command-line option tells Curl to send data to the server in multipart/form-data format.

How to upload multiple files at once using Curl?

To post multiple files to the server at the same time, add additional -F option for each file name.

Curl upload multiple files
$ curl [URL]
   -F file1=@filename1
   -F file2=@filename2
   -F file3=@filename3

See also

Generate code snippets for PHP and other programming languages

Convert your Curl POST File request to the PHP, JavaScript/AJAX, Node.js, Curl/Bash, Python, Java, C#/.NET code snippets using the PHP code generator.