I want to check the file's size of local drives on windows OS
.But the native PHP function filesize()
only work when the file size less than 2GB
. The file which greater than 2GB
will return the wrong number.So,is there other way to get the file size which greater than 2GB?
Thank you very much!!
this function works for any size:
note: this function maybe litte slow for files > 2gb
(taken from php comments)
PHP function to get the file size of a local file with insignificant memory usage:
In first line of code,
$file
is opened in read-only mode and attached to the$fp
handle.In second line, the pointer is moved with fseek() to the end of
$file
.Lastly, ftell() returns the byte position of the pointer in
$file
, which is now the end of it.The fopen() function is binary-safe and it's apropiate for use even with very large files.
The above code is also very fast.
This function returns the size for files > 2GB and is quite fast.
You can always use the system's file size method.
For Windows: Windows command for file size only?
For Linux
You would run these through the
exec
php command.To riff on joshhendo's answer, if you're on a Unix-like OS (Linux, OSX, macOS, etc) you can cheat a little using
ls
:trim()
is there to remove the carriage return at the end. What's left is a string containing the full size of the file on disk, regardless of size or stat cache status, with no human formatting such as commas.Just be careful where the data in
$fullPathToFile
comes from...when making system calls you don't want to trust user-supplied data. Theescapeshellarg
will probably protect you, but better safe than sorry.