Reputation: 1318
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
Upvotes: 8
Views: 8729
Reputation: 4017
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).
Upvotes: 17
Reputation: 1
You will need to try a different method, the http:// stream wrapper does not support stat()
which is needed for the filesize() function.
This question has some options you might use, using curl
to make a HEAD
request and inspect the Content-Length
header.
Upvotes: 0
Reputation: 3461
That error usually means the supplied URL does not return a valid image. When I try to visit http://myapp/wp-content/uploads/2017/05/Screen-Shot-2017-05-08-at-10.35.54.png
it does not show an image in my browser. Double check that the returned URL from $file->guid;
is correct.
Upvotes: 0