PHP filesize over 4Gb
Asked Answered
P

4

6

I'm running a Synology NAS Server, and I'm trying to use PHP to get the filesize of files.
I'm trying to find a function that will successfully calculate the filesize of files over 4Gb.

filesize($file); only works for files <2Gb
sprintf("%u", filesize($file)); only works for files <4Gb

I also tried another function that I found on the php manual, but it doesn't work properly.
It randomly works for certain file sizes but not for others.

function fsize($file) {
  // filesize will only return the lower 32 bits of
  // the file's size! Make it unsigned.
  $fmod = filesize($file);
  if ($fmod < 0) $fmod += 2.0 * (PHP_INT_MAX + 1);

  // find the upper 32 bits
  $i = 0;

  $myfile = fopen($file, "r");

  // feof has undefined behaviour for big files.
  // after we hit the eof with fseek,
  // fread may not be able to detect the eof,
  // but it also can't read bytes, so use it as an
  // indicator.
  while (strlen(fread($myfile, 1)) === 1) {
    fseek($myfile, PHP_INT_MAX, SEEK_CUR);
    $i++;
  }

  fclose($myfile);

  // $i is a multiplier for PHP_INT_MAX byte blocks.
  // return to the last multiple of 4, as filesize has modulo of 4 GB (lower 32 bits)
  if ($i % 2 == 1) $i--;

  // add the lower 32 bit to our PHP_INT_MAX multiplier
  return ((float)($i) * (PHP_INT_MAX + 1)) + $fmod;
}

Any ideas?

Pathic answered 26/6, 2011 at 4:52 Comment(0)
C
14

You are overflowing PHP's 32-bit integer. On *nix, this will give you the filesize as a string:

<?php $size = trim(shell_exec('stat -c %s '.escapeshellarg($filename))); ?>
Crapulous answered 26/6, 2011 at 5:11 Comment(1)
Great! The filename is escaped! That even seem secure to me.Roper
M
1

How about executing a shell command like:

<?php

echo shell_exec("du 'PATH_TO_FILE'");

?>

where PATH_TO_FILE is obviously the path to the file relative to the php script

you will most probably do some regex to get the filesize as a standalone as it returns a string like:

11777928    name_of_file.extention
Mainmast answered 26/6, 2011 at 5:4 Comment(1)
When I try to just echo that it says Warning: shell_exec(): Cannot execute using backquotes in Safe Mode in /volume1/web/required/function.php on line 153. I tried changing safe mode on/offPathic
B
0

Here is one complete solution what you can try: https://mcmap.net/q/1278156/-php-filesize-on-files-gt-2-gb

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);
Bridgettbridgette answered 21/1, 2018 at 4:11 Comment(0)
A
0

This function can run on the 32-bit Linux

function my_file_size($file){

    if ( PHP_INT_MAX > 2147483647){  //64bit
        return  filesize($file);
    }

    $ps_cmd = "/bin/ls -l $file";
    exec($ps_cmd, $arr_output, $rtn);
    ///bin/ls -l /data/07f2088a371424c0bdcdca918a3008a9cbd74a25.ic2
    //-rw-r--r-- 1 resin resin 269484032 9月   9 21:36 /data/07f2088a371424c0bdcdca918a3008a9cbd74a25.ic2

    if($rtn !=0) return floatval(0);
    preg_match("/^[^\s]+\s+\d+\s+[^\s]+\s+[^\s]+\s+(\d+)\s+/", $arr_output[0], $matches);
    if(!empty($matches)){
        return  floatval($matches[1]);
    }
    return floatval(0);
}
Auctioneer answered 27/10, 2020 at 9:6 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.