Php - Getting file size with an url - php

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

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).

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.

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.

Related

Getting an image with the ASHX extension

I am crawling some websites for images. However, some of these sites have the .ashx extension, which makes me unable to determine the sizes of the images.
I am using getimagesize():
$imgsize = getimagesize($url);
This results in the following error:
getimagesize(url): failed to open stream: No such file or directory
How can I get around this, and check the size of the image?
I think you can attempt to use the code from this answer, although I think it should work the way you have it already:
// From #dynamic: https://stackoverflow.com/a/10971333/697370
$cont = file_get_contents($url);
$r = imagecreatefromstring($cont);
imagesx($r);
imagerx($r);
Your original code may not be working if:
You don't have allow_url_fopen enabled (unlikely as I think that would also cause you to not be able to fetch regular images with getimagesize()), or
The .ashx script is correctly generating an image, but is not sending the correct headers which is causing getimagesize to fail.
There may be other causes, but those are the only two I can think of.

Getting filetype/filesize from stream

I'm getting file contents from a stream with
$src_file = file_get_contents("php://input");
but I need to know the filetype and filesize of the file as well. This doesn't work:
$src_type = filetype("php://input");
$src_size = count($src_file);
I suppose I could write the file and then call filetype/filesize on that, but is there a way to get filetype and filesize from a stream or contents of a variable?
To get the full length of a string, use strlen, this must work.
There's no simple way to sniff the filetype of a stream, and you shouldn't try to do this.
But since you are dealing with php://input, likely from a PUT request, clients should generally set a proper mimetype in the Content-Type header. You can get this from $_SERVER['CONTENT_TYPE'] or $_SERVER['HTTP_CONTENT_TYPE'] depending on the sapi.
In a stream, the 'type' of the file does not make any sense. A stream just represents an input of bits. So the filetype call will fail, because PHP simply cannot find the type. But php://input is not a file. It is the input given to php. In linux for example, it would be able to do the following:
ls | php file.php
In which case the input to PHP is not a file, but the output of another program.

how can i check if a given URL is an image?

I thought of using getimagesize($url); but there are still many cases where i can access the image through the browser but the same image returns nothing from getimagesize($url);
$url = 'http://lp.hm.com/hmprod?set=key[source],value[/model/2012/P01 06826 05102 04 0026 4.jpg]&set=key[rotate],value[]&set=key[width],value[]&set=key[height],value[]&set=key[x],value[]&set=key[y],value[]&set=key[type],value[STILL_LIFE_FRONT]&call=url[file:/product/large] ';
Just check the Content-Type header for the string image.
Just use the function get_headers(): http://php.net/manual/en/function.get-headers.php
You can also use curl if it's available on your system, details here: Get mime type of external file using cURL and php
To use getimagesize() you need to download the url and save it as a local file. Then pass the string of the filename stored locally to getimagesize().

PHP filesize of dynamically chosen file

I have a php script that needs to determine the size of a file on the file system after being manipulated by a separate php script.
For example, there exists a zip file that has a fixed size but gets an additional file of unknown size inserted into it based on the user that tries to access it. So the page that's serving the file is something like getfile.php?userid=1234.
So far, I know this:
filesize('getfile.php'); //returns the actual file size of the php file, not the result of script execution
readfile('getfile.php'); //same as filesize()
filesize('getfile.php?userid=1234'); //returns false, as it can't find the file matching the name with GET vars attached
readfile('getfile.php?userid=1234'); //same as filesize()
Is there a way to read the result size of the php script instead of just the php file itself?
filesize
As of PHP 5.0.0, this function can also be used with some URL
wrappers.
something like
filesize('http://localhost/getfile.php?userid=1234');
should be enough
Someone had posted an option for using curl to do this but removed their answer after a downvote. Too bad, because it's the one way I've gotten this to work. So here's their answer that worked for me:
$ch = curl_init('http://localhost/getfile.php?userid=1234');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); //This was not part of the poster's answer, but I needed to add it to prevent the file being read from outputting with the requesting script
curl_exec($ch);
$size = 0;
if(!curl_errno($ch))
{
$info = curl_getinfo($ch);
$size = $info['size_download'];
}
curl_close($ch);
echo $size;
The only way to get the size of the output is to run it and then look. Depending on the script the result might differ though for practical use the best thing to do is to estimate basd on your knowledge. i.e. if you have a 5MB file and add another 5k user specific content it's still about 5MB in the end etc.
To expand on Ivan's answer:
Your string is 'getfile.php' with or without GET parameters, this is being treated as a local file, and therefore retrieving the filesize of the php file itself.
It is being treated as a local file because it isn't starting with the http protocol. See http://us1.php.net/manual/en/wrappers.php for supported protocols.
When using filesize() I got a warning:
Warning: filesize() [function.filesize]: stat failed for ...link... in ..file... on line 233
Instead of filesize() I found two working options to replace it:
1)
$headers = get_headers($pdfULR, 1);
$fileSize = $headers['Content-Length'];
echo $fileSize;
2)
echo strlen(file_get_contents($pdfULR));
Now it's working fine.

Get size of POST-request in PHP

Is there any way to get size of POST-request body in PHP?
As simple as:
$size = (int) $_SERVER['CONTENT_LENGTH'];
Note that $_SERVER['CONTENT_LENGTH'] is only set when the HTTP request method is POST (not GET). This is the raw value of the Content-Length header, as specified in RFC 7230.
In the case of file uploads, if you want to get the total size of uploaded files, you should iterate over the $_FILE array to sum each $file['size']. The exact total size might not match the raw Content-Length value due to the encoding overhead of the POST data. (Also note you should check for upload errors using the $file['error'] code of each $_FILES element, such as UPLOAD_ERR_PARTIAL for partial uploads or UPLOAD_ERR_NO_FILE for empty uploads. See file upload errors documentation in the PHP manual.)
If you're trying to figure out whether or not a file upload failed, you should be using the PHP file error handling as shown at the link below. This is the most reliable way to detect file upload errors:
http://us3.php.net/manual/en/features.file-upload.errors.php
If you need the size of a POST request without any file uploads, you should be able to do so with something like this:
$request = http_build_query($_POST);
$size = strlen($request);
My guess is, it's in the $_SERVER['CONTENT_LENGTH'].
And if you need that for error detection, peek into $_FILES['filename']['error'].
This might work :
$bytesInPostRequestBody = strlen(file_get_contents('php://input'));
// This does not count the bytes of the request's headers on its body.
I guess you are looking for $HTTP_RAW_POST_DATA

Categories