DevKB
Web Development Knowledge Base
HOME | TOP 20 | WRITE AN ARTICLE |
Sections :



RSS RSS Feed

You too, please publish your useful code snippets in any programming language :
write an article !


Valid HTML 4.0 Transitional

Plateforme d'envoi de gros fichiers en ligne
Dépannage site web
Blog infogérance
Hébergement e-mail

Olivier Ligny - - 19/08/2012 - vue 41038 fois

Get file size in PHP for files larger than 2 GB

You may encounter some problems when using the filesize() function of PHP on large files (> 2 GB).
This is because of 32 bits PHP internal limitations.

Here is a workaround to get file size with no limit.
You may need to disable safe_mode in order to let the function call exec() method.

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;
}

Jan Kuchar - 05/02/2016
You can also use Big File Tools - https://github.com/jkuchar/BigFileTools. Library for manipulating files over 2GB in PHP. More details in my original answer on stackoverflow: http://stackoverflow.com/a/35233556/631369


Write a comment :
Your name :     E-mail (optional) :

AntiSpam : what animal is visible on this picture ? :

Nos partenaires : iPhone 8 Cases & Protection