Are there alternative methods to retrieve the file size of a remote file in PHP without using fopen?

When retrieving the file size of a remote file in PHP without using fopen, an alternative method is to use the cURL library. cURL allows you to make HTTP requests and retrieve information about remote files without opening them directly. By sending a HEAD request to the file URL, you can retrieve the headers containing the file size information.

<?php
$url = 'https://www.example.com/remote-file.txt';

$ch = curl_init($url);
curl_setopt($ch, CURLOPT_NOBODY, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, true);

$response = curl_exec($ch);

if ($response !== false) {
    $size = curl_getinfo($ch, CURLINFO_CONTENT_LENGTH_DOWNLOAD);
    echo "File size: $size bytes";
} else {
    echo "Error retrieving file size";
}

curl_close($ch);
?>