How can I get file size in PHP without opening the full file?

Using fopen() or similar methods load the full file when checking for file size which is a poor implementation, because when using large files, it is AWESOME(!). My file sizes could be as large as 2/3GB. However, in other places, I have seen tutorials like using cURL header responses. But I am on IIS, Windows Server 2008 R2, and IIS does not return the Content-Length header response. So the cURL method is useless.

How can I get file size in PHP without opening the file?
Md Moinul IslamAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Dave BaldwinFixer of ProblemsCommented:
Try filesize ( string $filename )

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial

// outputs e.g.  somefile.txt: 1024 bytes

$filename = 'somefile.txt';
echo $filename . ': ' . filesize($filename) . ' bytes';

Md Moinul IslamAuthor Commented:
Thanks. But the filesize() function does not work good for large files.  I have found the solutions, finally.


CertaiN, commented

function remote_filesize($url) {
    static $regex = '/^Content-Length: *+\K\d++$/im';
    if (!$fp = @fopen($url, 'rb')) {
        return false;
    if (
        isset($http_response_header) &&
        preg_match($regex, implode("\n", $http_response_header), $matches)
    ) {
        return (int)$matches[0];
    return strlen(stream_get_contents($fp));

I think this works good, so far. I will check if there are any problems with that.
Angular Fundamentals

Learn the fundamentals of Angular 2, a JavaScript framework for developing dynamic single page applications.

try this one , I hope you are using OS as linux

function filesize2($path) {
  if(@is_dir($path)) {
    return 0;
  $sizeInBytes = @filesize($path);
  if($sizeInBytes===false) {
    $command = "/bin/ls -l ".escapeshellarg($path);
    $ret = trim(exec($command));
    if(!substr_count($ret, "ls:") && !substr_count($ret, "Aucun fichier")) {
      $ret = str_replace("\t", " ", $ret);
      $ret = str_replace("  ", " ", $ret);
      $ret = str_replace("  ", " ", $ret);
      $ret = str_replace("  ", " ", $ret);
      $arr = explode(" ", $ret);
      $sizeInBytes = $arr[4];
  return $sizeInBytes;
Md Moinul IslamAuthor Commented:
@insoftservice, perhaps, you didn't read my question in detail. I am on windows server 2008 R2

My previous comment did not work as I expected. Search is on!
Dave BaldwinFixer of ProblemsCommented:
What problem are you having with 'filesize()'?
Md Moinul IslamAuthor Commented:
filesize() will load the whole file into memory to return the file size, therefore utilizing server resources+consumes a lot of time. It's ok for the small files, i think
Ray PaseurCommented:
From the man page:

Note: Because PHP's integer type is signed and many platforms use 32bit integers, some filesystem functions may return unexpected results for files which are larger than 2GB.

Also from the man page, this comment which seems to be the basis of your solution:

From the stream_get_contents() man page:

stream_get_contents — Reads remainder of a stream into a string

In other words, the solution you propose to use, as you wrote in the question, "load the full file when checking for file size which is a poor implementation..."

Executive summary: There is something in your design pattern that does not make sense.  Perhaps you need to consider a database that will hold the details about these very large files.  Or perhaps you want to consider whether Windows is a useful platform for big data applications (it's not).  A different OS may be a more appropriate solution when you must deal with very large files.
Md Moinul IslamAuthor Commented:
Ray Paseur, thanks a lot for your valuable comments. I can understand that it's windows. But still, isn't there a way to get the file size, like when we click on windows explorer / file properties. In some PHP based file managers, the file information is displayed in the file view panel. Currently I am investigating the source codes of some of those to figure out how those file manager's display file size information.
Dave BaldwinFixer of ProblemsCommented:
The limit (which is mentioned on the man page) is that PHP is a 32-bit application and file sizes that are greater than 2GB cause problems.  If you can find a 64-bit version of PHP, it probably won't have that problem.  But you may find it doesn't have all the extensions you might want.  I'd try to check it for you but I don't have any files that big.
Ray PaseurCommented:
filesize() will load the whole file into memory to return the file size, therefore utilizing server resources+consumes a lot of time
In my tests, this does not appear to be true.  Here are the two scripts I used to test.  The first makes a "big" file, the second uses filesize() and then reads the "big" file.  Both scripts output the peak memory usage, allowing you to see that the peak memory usage before and after filesize() is unchanged.  Perhaps you can run these scripts or some variant of them to see what is really happening on your system.

Make a "big" file
<?php // demo/make_big_file.php

// SEE

$a = memory_get_peak_usage();
$x = str_repeat('Hello World' . PHP_EOL, 6000);
$b = memory_get_peak_usage();
file_put_contents('storage/big.txt', $x);
$c = memory_get_peak_usage();
var_dump($a, $b, $c);

Open in new window

Outputs: int(229864) int(300816) int(373448)

Check filesize() and read "big" file:
<?php // demo/filesize.php

// SEE

$a = memory_get_peak_usage(TRUE);
$s = filesize('storage/big.txt');
$b = memory_get_peak_usage(TRUE);
$x = file_get_contents('storage/big.txt');
$c = memory_get_peak_usage(TRUE);
var_dump($a, $b, $c);

Open in new window

Outputs: int(262144) int(262144) int(524288)

Executive summary: Dave Baldwin gave you the right answer within minutes of seeing the question.
Md Moinul IslamAuthor Commented:
Thanks Ray for your valuable comments. Ok. I am giving that another try.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.