PHP code for Curl Download File Example
This PHP code snippet was generated automatically for the Curl Download File example.<< Back to the Curl Download File example
What is Curl?
Curl is an open source command line utility and cross-platform libcurl library for sending HTTP requests from clients to servers. Curl supports all popular Internet protocols including HTTP and HTTPS. Curl has built-in support for validating certificates, SSL, HTTP Cookies and is used to debug network requests and API calls. Curl works on all modern platforms including Windows, macOS and Linux.
How to download files using Curl?
By default, when you request a resource from the server using Curl, it downloads the resource and displays its contents on the screen. When using Curl, you must explicitly request that the downloaded resource be saved to disk by using the --output (-o) or --remote-name (-O) command-line options. The file will be saved to the current directory that you are working in. To save the file in a different directory, change the working directory before running the Curl command. If the file already exists, it will be overwritten.
If you want to give the downloaded file a specific name, you must use the --output or -o option with the following syntax:
Curl Download File Example
The following is an example of downloading a file using Curl:
How to save the file with its original name?
To save the downloaded file with the same name as specified in the URL, use the --remote-name (or -O) command line parameter. Curl will only take file part and ignore the path. Below is an example of downloading a file using Curl, keeping its original name:
How to download multiple files using Curl?
To download multiple files using Curl, you can specify multiple URLs and filenames in one Curl command. Curl will save the first URL under the first file name, the second URL under the second file name, etc. Below is an example of downloading multiple files using Curl:
How can I see the download progress?
By default, Curl shows no progress when downloading a file. To see the download progress, use the "-#" flag. The following is an example of printing download progress using the Curl flag:
How to enable silent download mode in Curl?
If you don't want to see any information while downloading the file, you can turn on silent mode to prevent Curl from printing anything. The silent mode can be enabled with the --silent (-s) command-line parameter. This can be useful when using Curl in automation scripts. Below is an example of silent downloading of a file in Curl:
How to limit download speed in Curl?
Curl allows you to limit the maximum download speed using the –limit-rate flag. This can be useful when using Curl on slow Internet connections. Download speed is measured in bytes per second. If you want to specify the speed in kilobytes, add the suffix "k" or "K", or "m" or "M" for megabytes. The suffixes (k, M, G) are 1024 in size. In this Curl Download File example, we limit the download speed to 100 kilobytes per second.