Curl File Upload Example – How to upload files with Curl

In this article, I have covered different examples of CURL file uploa. cURL is a powerful command-line tool used to transfer data to or from a server.

This article discusses one of the many features of cURL: uploading files to a remote server. This process, typically done using the HTTP POST method, is made simple and easily accomplishable with cURL through a few command-line arguments.

Curl File Upload Example

Table of Contents

 

Syntax and Explanation

To upload a file using cURL, use the following basic syntax:

  curl -F "parameter_name=@local_file" URL
  • -F: This flag stands for form, allowing you to specify the multipart POST data.
  • parameter_name: The name of the form field for file upload.
  • @local_file: The local file you wish to upload, preceded by the ‘@’ character.
  • URL: The destination URL where the file will be uploaded.

Practical Curl File upload Examples

Example 1: Basic File Upload

  curl -F "file=@/path/to/yourfile.png" http://example.com/upload

This command uploads a PNG file to the server at example.com using the form field name ‘file’.


Example 2: Upload With Custom Field Name

  curl -F "myImage=@/path/to/yourfile.jpg" http://example.com/upload

The command uploads a JPEG file with a custom field name ‘myImage’.


Example 3: Multiple File Upload

  curl -F "file1=@/path/to/firstfile.png" -F "file2=@/path/to/secondfile.pdf" http://example.com/upload

The command concurrently uploads two files to the server, assigning distinct field names to each for differentiation.


Example 4: Upload With Different Form Fields

  curl -F "image=@/path/to/image.png" -F "description=Profile picture" http://example.com/upload

This process simultaneously uploads a file and includes additional form field data, such as a description, ensuring a comprehensive submission of both the file and its relevant information.


Example 5: Authentication With File Upload

  curl -u username:password -F "file=@/path/to/securefile.zip" http://example.com/upload

The command uploads a file to a server that requires basic HTTP authentication.

 

Conclusive Summary

In this tutorial, the essentials of using cURL to upload files to a server were covered. It began with explanations of the syntax and then moved on to practical examples. The examples demonstrated basic file uploads, as well as multi-file and authenticated uploads. By using the “curl file upload” command, developers are able to easily transfer files from the command line to remote servers, streamlining work processes and enabling efficient file management.

References