PHP filesize () In files> 2 GB

I am doing how to get a valid file for s> = 2 GB file in PHP.

Example
Here I check the size of a file that has 3,827,394,560 bytes with the filesize () function:

echo "The file is " . filesize('C:\MyFile.rar') . " bytes.";

Result
This is what it returns:

The file has a size of -467572736 bytes.


PHP Background uses signed integers, which means that the maximum number that it can represent is 2,147,483,647 (+/- 2 GB).
Here it is limited.

+5
source share
7 answers

http://us.php.net/manual/en/function.filesize.php#102135 2 PHP, .

, filesize "" , +, , , 2GB ( "" ).

+4

, , -, , "" COM FileObject. , .

:

function real_filesize($file_path)
{
    $fs = new COM("Scripting.FileSystemObject");
    return $fs->GetFile($file_path)->Size;
}

:

$file = 'C:\MyFile.rar';
$size = real_filesize($file);
echo "The size of the file is: $size";

: 3,827,394,560 .

+3

, , GitHub github.com/eladkarako/download.eladkarako.com.

, , (* ) filesize , HTML- , (* ) , hmm..., , .

, Apache .htaccess ( PHP), Content-Length .

, .htaccess, Content-Length CORS.. .

no jQuery , Samsung FTP , 1,5- MILUIM.. , ;)

+1

"" , .

, , .

:

include_once 'class.os.php';
include_once 'function.filesize.32bit.php';

// Must be real path to file
$file = "/home/username/some-folder/yourfile.zip";
echo get_filesize($file);

, :

  • , shell_exec PHP. , shell real realize.
  • 64 filesize()
  • 32bit "chunking" .

!

Alfter , PHP , 2 , , .

!

, , script , . , shell_exec, .

P.S.

- , .

0

, , PHP x64 5.5.38 () 7.x.

2 , .

, C/C++ PHP "php_filesize.dll", C/C++ , , UTF-8

: http://www.jobnik.org/files/PHP/php_filesize.zip

:

:

0 - GetFileAttributesEx

1 - CreateFile

2 - FindFirstFile

-1 - stat64 ( )

$ fsize = php_filesize ("filepath", $ method_optional);

  • 9 PetaByte

:

FileSize: , C++?

UTF-8: https://github.com/kenjiuno/php-wfio

0

, Linux, , , :

exec("stat --format=\"%s\" \"$file\"");

, , , , , , .

, , 3,6 .

0

To get the correct file size, I often use this piece of code that I wrote a few months ago. My code uses: exec/ com/ statwhere possible. I know its boundaries, but it is a good starting point. The best idea is to use it filesize()on a 64-bit architecture.

<?php
  ######################################################################
  # Human size for files smaller or bigger than 2 GB on 32 bit Systems #
  # size.php - 1.3 - 21.09.2015 - Alessandro Marinuzzi - www.alecos.it #
  ######################################################################
  function showsize($file) {
    if (strtoupper(substr(PHP_OS, 0, 3)) == 'WIN') {
      if (class_exists("COM")) {
        $fsobj = new COM('Scripting.FileSystemObject');
        $f = $fsobj->GetFile(realpath($file));
        $size = $f->Size;
      } else {
        $size = trim(@exec("for %F in (\"" . $file . "\") do @echo %~zF"));
      }
    } elseif (PHP_OS == 'Darwin') {
      $size = trim(@exec("stat -f %z " . $file));
    } else {
      $size = trim(@exec("stat -c %s " . $file));
    }
    if ((!is_numeric($size)) || ($size < 0)) {
      $size = filesize($file);
    }
    if ($size < 1024) {
      echo $size . ' Byte';
    } elseif ($size < 1048576) {
      echo number_format(round($size / 1024, 2), 2) . ' KB';
    } elseif ($size < 1073741824) {
      echo number_format(round($size / 1048576, 2), 2) . ' MB';
    } elseif ($size < 1099511627776) {
      echo number_format(round($size / 1073741824, 2), 2) . ' GB';
    } elseif ($size < 1125899906842624) {
      echo number_format(round($size / 1099511627776, 2), 2) . ' TB';
    } elseif ($size < 1152921504606846976) {
      echo number_format(round($size / 1125899906842624, 2), 2) . ' PB';
    } elseif ($size < 1180591620717411303424) {
      echo number_format(round($size / 1152921504606846976, 2), 2) . ' EB';
    } elseif ($size < 1208925819614629174706176) {
      echo number_format(round($size / 1180591620717411303424, 2), 2) . ' ZB';
    } else {
      echo number_format(round($size / 1208925819614629174706176, 2), 2) . ' YB';
    }
  }
?>

<?php include("php/size.php"); ?>

<?php showsize("files/VeryBigFile.tar"); ?>

Hope this helps.

-1
source

All Articles