What alternative methods can be used to restrict the size of downloaded files when using CURL in PHP?

When using CURL in PHP to download files, it's important to restrict the size of downloaded files to prevent excessive memory usage or potential security risks. One alternative method to restrict the size of downloaded files is by setting the CURLOPT_FILESIZE_LIMIT option in the CURL request. This option allows you to specify the maximum size of the file to be downloaded in bytes.

// Initialize a CURL session
$ch = curl_init();

// Set the URL to download the file from
curl_setopt($ch, CURLOPT_URL, 'http://example.com/file-to-download');

// Set the maximum file size limit to 10MB
curl_setopt($ch, CURLOPT_FILESIZE_LIMIT, 10485760);

// Execute the CURL request
curl_exec($ch);

// Close the CURL session
curl_close($ch);