Home > Backend Development > PHP Tutorial > How Can Curl Stream Large File Downloads Directly to Disk to Avoid Memory Issues?

How Can Curl Stream Large File Downloads Directly to Disk to Avoid Memory Issues?

DDD
Release: 2024-12-05 07:33:10
Original
392 people have browsed it

How Can Curl Stream Large File Downloads Directly to Disk to Avoid Memory Issues?

Handling Large File Downloads with Curl: Streaming to Disk

Downloading large files using curl can be challenging due to memory constraints. The traditional method, which reads the entire file into memory before writing it to disk, can cause performance issues. To overcome this limitation, consider streaming the file directly to disk.

Here's a solution that employs the curl_setopt() function to configure the CURLOPT_FILE option. This option specifies a file pointer where curl can directly write the downloaded data:

set_time_limit(0); // Disable PHP time limit
// Open a file for writing
$fp = fopen(dirname(__FILE__) . '/localfile.tmp', 'w+');
// Initialize curl
$ch = curl_init(str_replace(" ", "%20", $url));
// Set timeout to a high value
curl_setopt($ch, CURLOPT_TIMEOUT, 600);
// Write curl response to file
curl_setopt($ch, CURLOPT_FILE, $fp); 
// Follow any redirects
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
// Execute curl
curl_exec($ch); 
// Close curl and file handlers
curl_close($ch);
fclose($fp);
Copy after login

In this improved code:

  • set_time_limit(0) removes the PHP time limit for large downloads.
  • fopen opens the destination file for writing.
  • curl_init initializes curl with the given URL.
  • CURLOPT_TIMEOUT sets a high timeout value for large downloads.
  • CURLOPT_FILE specifies the file pointer where the downloaded data is written.
  • CURLOPT_FOLLOWLOCATION allows the script to follow redirects, ensuring the download completes successfully.
  • curl_exec executes curl to download the file.
  • curl_close and fclose close the curl and file handlers, respectively.

By using this technique, curl can stream the downloaded data directly to disk, bypassing the memory constraints and enabling efficient handling of large files.

The above is the detailed content of How Can Curl Stream Large File Downloads Directly to Disk to Avoid Memory Issues?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template