Php - Get file size using url

I am trying to get the image file size from a remote url, I am trying to do it like this:

$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 to execute http: //myApp/wp-content/uploads/2017/05/Screen-Shot-2017-05-08-at-10.35.54.png

+4
source share
3 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, for example $headers['Content-Type'], which may come in handy if you are dealing with images ( documentation ).

+1

, URL- . http://myapp/wp-content/uploads/2017/05/Screen-Shot-2017-05-08-at-10.35.54.png, . URL $file->guid;.

0

You will need to try another method, http: // the stream shell does not support stat(), which is necessary for the filesize () function .

This question contains some parameters that you can use curlto make a request HEADand check the title Content-Length.

0
source

Source: https://habr.com/ru/post/1679801/


All Articles