Home >Backend Development >PHP Tutorial >How Can I Stream Large File Downloads with cURL to Avoid Memory Issues?

How Can I Stream Large File Downloads with cURL to Avoid Memory Issues?

DDD
DDDOriginal
2024-12-01 13:35:10275browse

How Can I Stream Large File Downloads with cURL to Avoid Memory Issues?

Streaming Large File Downloads with CURL

Downloading large remote files can be a challenge, especially when the files cannot fit within the available memory on the server. This can be a frequent issue when using CURL, which by default stores file data in memory before writing it to disk.

Fortunately, there is a solution that allows you to stream the file directly to disk instead. To achieve this, make the following adjustments to your code:

<?php
set_time_limit(0);

// Open a file pointer for writing
$fp = fopen(dirname(__FILE__) . '/localfile.tmp', 'w+');

// Set up CURL options
$ch = curl_init(str_replace(" ", "%20", $url));

// Set timeout to a high enough value
curl_setopt($ch, CURLOPT_TIMEOUT, 600);

// Write CURL response to the file pointer
curl_setopt($ch, CURLOPT_FILE, $fp);

// Enable following of location headers
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);

// Execute CURL
curl_exec($ch);

// Close CURL and the file pointer
curl_close($ch);
fclose($fp);

By setting CURLOPT_FILE to the file pointer, you are instructing CURL to write the downloaded data directly to the file on disk instead of storing it in memory. This ensures that even very large files can be downloaded smoothly without memory limitations.

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

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