It seems that, the problem occurs due to signaling that the PHP imposes on those of the type whole, and many platforms use 32 bits, reason why the filesize() sometimes returns unexpected results for files larger than 2GB.
As for the explanation of why this expression is more appropriate, it is a bit complicated to answer, since several users have tried in various ways to write their own and even more complex methods, to get the actual size of a file.
It prints the result of filesize as UNSIGNED INT so it can be until 4GB.
The Reason is, SIGNED INT runs until 2GB and flips to -2GB watch following:
Translation:
This prints the result of _"filesize" as "unsigned whole", so it can be up to 4GB. It happens because, "signed whole" run up to 2GB and turn to -2GB, see:
file<2GB = SIGNED: 1048576512 UNSIGNED: 1048576512
file>2GB = SIGNED: -2100140103 UNSIGNED: 2194827193
file>4GB = SIGNED: -100662784 UNSIGNED: 4194304512
This text quoted above was taken from any directory of PHP, in it the user explains why of the function. However it does not say if it is the most indicated or not.
In my view, it is very likely that this expression is being used because it returns negative values for files between 2GB and 4GB, which can still be corrected with some calculation, and returns a definite and incorrigible value for files above 4GB. In fact it was kind of alarming the example being only in the document note in Portuguese, but, the example already existed in the contribution notes.
On the page of PHP usually the examples we find there are the simplest, it does not mean that it is the only way to get the actual size of a file. It’s very likely that this will require some testing on your part, because you don’t find much information about why you use the sprintf.
He fails in some particular situation?
Some users reported that there were failures on some x86 architecture-based systems, and some problems reported on x64 systems, so it is very likely that there are still some errors. Even if you fail, you will return one E_WARNING
or simply FALSE
.
It has no accuracy as to the actual weight of the file?
The accuracy is good, returns the actual size in bytes.
Or the code actually works to convert the weight into integers to a numerical string?
Yes, it works, this is the return I got in the last result:
$file = "ficheiro.zip";
var_dump(sprintf("%u", filesize($file)));
Retorno: string(4) "5209" (5.08KB)
Retorno: string(10) "2092964971" (1.94GB)
There are several examples available on how to get the actual file size for the most diverse platforms, some are even based on shell
, All you have to do is look for what suits you best. If you need more details, I believe the only solution will be to run isolated tests, and dig deeper.
Good luck.
References:
PHP.cz
PHP.tw
PHP.edu
Drupal.org
PHP.net
The problem of using Curl is if the file is 4gb the server consciously delay
– FABIO MATEUS
@FABIOMATEUS even defining the
curl_setopt($ch, CURLOPT_NOBODY, 1);
it takes time? If it takes time yet yes, then use the first solution, withstat -c
andfor %F in
which I have demonstrated in https://answall.com/a/183202/3635– Guilherme Nascimento