How to get file size from URL in PHP?

I’m trying to build a PHP script where I want to hide the original file URL using PHP. At this point, I need to find the size of a remote file by its URL. I tried the following code but it’s not working.

$url = 'https://example.com/file.mp4';
$filesize = filesize($url);

So I am wandaring If I can retrieve the file size without actually downloading the entire file?"

To get the file size of a remote file by its URL in PHP, you can use the get_headers() function and look for the Content-Length header in the response. Here’s an example code snippet that demonstrates how to do this:

$url      = 'https://example.com/file.mp4';
$header   = get_headers($url, true);
$filesize = isset($header ['Content-Length']) ? (int)$header ['Content-Length'] : 0;

Hope that solves your problem.