Are there any best practices or alternative methods for handling file size retrieval in PHP when working with absolute URLs?

When working with absolute URLs in PHP to retrieve file sizes, it is important to consider that using functions like file_get_contents() or curl can be inefficient and slow, especially for large files. A better approach is to use the HEAD method of HTTP requests, which retrieves only the headers of the file without downloading the entire content. This method is faster and more efficient for retrieving file sizes from absolute URLs.

function getFileSize($url) {
    $ch = curl_init($url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_NOBODY, true);
    curl_exec($ch);
    
    $size = curl_getinfo($ch, CURLINFO_CONTENT_LENGTH_DOWNLOAD);
    
    curl_close($ch);
    
    return $size;
}

$url = "https://www.example.com/file.zip";
$fileSize = getFileSize($url);

echo "File size: " . $fileSize . " bytes";