我想检查文件的大小在windows OS上的本地驱动器.But,本机PHP函数filesize()只有当文件大小小于2GB时才起作用。大于2GB的文件将返回错误的number.So,是否有其他方法可以获取大于2GB?的文件大小
非常感谢!
发布于 2011-03-04 08:42:29
您可以始终使用系统的文件大小方法。
适用于Windows的:Windows command for file size only?
关闭
@echo
回声%~z1
对于Linux
stat -c %s filenam您可以通过exec php命令运行这些命令。
发布于 2019-02-08 20:04:19
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()函数是二进制安全的,即使是非常大的文件也适合使用。
上面的代码也非常快。
发布于 2011-03-04 08:44:05
此函数适用于任何大小:
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;
}注意:对于大于2 2gb的文件,此函数可能会有一点慢
(摘自php评论)
https://stackoverflow.com/questions/5188385
复制相似问题