Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Php - Getting file size with an url

Tags:

php

laravel

I am trying to get a file size of an image from a remote url, I am trying to this like so:

$remoteUrl = $file->guid;
//remote url example: http://myApp/wp-content/uploads/2017/05/Screen-Shot-2017-05-08-at-10.35.54.png  

$fileSize = filesize($remoteUrl);

But, I get:

filesize(): stat failed for http://myApp/wp-content/uploads/2017/05/Screen-Shot-2017-05-08-at-10.35.54.png

like image 840
Leff Avatar asked Jun 21 '17 11:06

Leff


1 Answers

You can use HTTP headers to find the size of the object. The PHP function get_headers() can get them:

$headers = get_headers('http://myApp/wp-content/uploads/2017/05/Screen-Shot-2017-05-08-at-10.35.54.png', true);
echo $headers['Content-Length'];

This way you can avoid downloading the entire file. You also have access to all other headers, such as $headers['Content-Type'], which can come in handy if you are dealing with images (documentation).

like image 150
Asur Avatar answered Sep 20 '22 13:09

Asur