得到文件的大小超过2GB,其更大的PHP中的最佳方式是什么?

问题描述 投票:2回答:7

我要检查本地驱动器的文件的大小上windows OS.But本地PHP函数filesize()只工作当文件大小超过2GB少。该文件,其中大于2GB将返回错误number.So,有没有其他的方式来获取文件尺寸大于2GB?

非常感谢你!!

php filesize
7个回答
4
投票

您可以随时使用该系统的文件大小的方法。

对于Windows:Windows command for file size only?

@回响

回声%〜Z1

对于Linux

stat -c %s filenam

您将通过exec PHP命令运行这些。


1
投票

此功能适用于任何尺寸:

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

注意:这个功能也许豆蔻的文件慢> 2GB

(从PHP的意见采取)


1
投票

PHP函数来获取与微不足道的内存使用本地文件的文件大小:

function get_file_size ($file) {
   $fp = @fopen($file, "r");
   @fseek($fp,0,SEEK_END);
   $filesize = @ftell($fp);
   fclose($fp);
   return $filesize;
}

在第一行代码,$file在只读模式打开和附接至手柄$fp。 在第二行中,指针移动与fseek()$file的末尾。 最后,ftell()返回指针在$file,这是现在的它的端部的字节位置。 该fopen()函数是二进制安全的,这是apropiate即使有非常大的文件使用。 上面的代码也非常快。


0
投票

如果你运行的是Linux服务器,使用系统命令。

$last_line = system('ls');

是如何使用它的例子。如果您要更换“LS”有:

du <filename>

然后它会在变量$ last_line返回文件大小的整数。例如:

472    myProgram.exe

意味着它是472 KB。您可以使用正则表达式来只获得数。我没有用过du命令那么多,所以你要玩它,并看看输出是什么文件> 2GB。

http://php.net/manual/en/function.system.php


0
投票
<?php
$files = `find / -type f -size +2097152`;
?>

0
投票

该函数返回大小的文件> 2GB,是相当快的。

function file_get_size($file) {
    //open file
    $fh = fopen($file, "r"); 
    //declare some variables
    $size = "0";
    $char = "";
    //set file pointer to 0; I'm a little bit paranoid, you can remove this
    fseek($fh, 0, SEEK_SET);
    //set multiplicator to zero
    $count = 0;
    while (true) {
        //jump 1 MB forward in file
        fseek($fh, 1048576, SEEK_CUR);
        //check if we actually left the file
        if (($char = fgetc($fh)) !== false) {
            //if not, go on
            $count ++;
        } else {
            //else jump back where we were before leaving and exit loop
            fseek($fh, -1048576, SEEK_CUR);
            break;
        }
    }
    //we could make $count jumps, so the file is at least $count * 1.000001 MB large
    //1048577 because we jump 1 MB and fgetc goes 1 B forward too
    $size = bcmul("1048577", $count);
    //now count the last few bytes; they're always less than 1048576 so it's quite fast
    $fine = 0;
    while(false !== ($char = fgetc($fh))) {
        $fine ++;
    }
    //and add them
    $size = bcadd($size, $fine);
    fclose($fh);
    return $size;
}

0
投票

要即兴的joshhendo's answer,如果你在一个类Unix操作系统(Linux操作系统,OSX,MACOS等),你可以欺骗一个使用ls一点:

$fileSize = trim(shell_exec("ls -nl " . escapeshellarg($fullPathToFile) . " | awk '{print $5}'"));

trim()有没有删除在最后的回车。剩下的就是包含文件在磁盘上的全尺寸,无论大小或STAT高速缓存状态,没有人的格式,如逗号的字符串。

只是要小心其中$fullPathToFile数据来自......让系统调用,当你不想信任用户提供的数据。该escapeshellarg可能会保护你的,但是有备无患。

© www.soinside.com 2019 - 2024. All rights reserved.