php filesize 单位,技术产品-PHP filesize() 函数

php filesize 单位,技术产品-PHP filesize() 函数filesize 作用 函数返回指定文件的大小 语法 filesize filename 参数 filename 必需 规定要检查的文件 返回值 返回文件大小的字节数 如果出错返回 FALSE 并生成一条 E WARNING 级的错误

大家好,我是讯享网,很高兴认识大家。

filesize

作用:函数返回指定文件的大小

语法:filesize(filename)

参数:

filename:必需。规定要检查的文件。

返回值:

返回文件大小的字节数,如果出错返回 FALSE 并生成一条 E_WARNING 级的错误。

filesize 示例

示例一<?php

// 输出类似:somefile.txt: 1024 bytes

$filename = 'somefile.txt';

echo $filename . ': ' . filesize($filename) . ' bytes';

?>

示例二<?php

function human_filesize($bytes, $decimals = 2) {

$sz = 'BKMGTP';

$factor = floor((strlen($bytes) - 1) / 3);

return sprintf("%.{$decimals}f", $bytes / pow(1024, $factor)) . @$sz[$factor];

}

?>

示例三<?php

/

* Converts bytes into human readable file size.

*

* @param string $bytes

* @return string human readable file size (2,87 Мб)

* @author Mogilev Arseny

*/

function FileSizeConvert($bytes)

{

$bytes = floatval($bytes);

$arBytes = array(

0 => array(

"UNIT" => "TB",

"VALUE" => pow(1024, 4)

),

1 => array(

"UNIT" => "GB",

"VALUE" => pow(1024, 3)

),

2 => array(

"UNIT" => "MB",

"VALUE" => pow(1024, 2)

),

3 => array(

"UNIT" => "KB",

"VALUE" => 1024

),

4 => array(

"UNIT" => "B",

"VALUE" => 1

),

);

foreach($arBytes as $arItem)


讯享网

{

if($bytes >= $arItem["VALUE"])

{

$result = $bytes / $arItem["VALUE"];

$result = str_replace(".", "," , strval(round($result, 2)))." ".$arItem["UNIT"];

break;

}

}

return $result;

}

?>

示例四<?php

/

* Return file size (even for file > 2 Gb)

* For file size over PHP_INT_MAX (2 147 483 647), PHP filesize function loops from -PHP_INT_MAX to PHP_INT_MAX.

*

* @param string $path Path of the file

* @return mixed File size or false if error

*/

function realFileSize($path)

{

if (!file_exists($path))

return false;

$size = filesize($path);

if (!($file = fopen($path, 'rb')))

return false;

if ($size >= 0)

{//Check if it really is a small file (< 2 GB)

if (fseek($file, 0, SEEK_END) === 0)

{//It really is a small file

fclose($file);

return $size;

}

}

//Quickly jump the first 2 GB with fseek. After that fseek is not working on 32 bit php (it uses int internally)

$size = PHP_INT_MAX - 1;

if (fseek($file, PHP_INT_MAX - 1) !== 0)

{

fclose($file);

return false;

}

$length = 1024 * 1024;

while (!feof($file))

{//Read the file until end

$read = fread($file, $length);

$size = bcadd($size, $length);

}

$size = bcsub($size, $length);

$size = bcadd($size, strlen($read));

fclose($file);

return $size;

}

以上就是技术产品|PHP filesize() 函数的内容,更多资讯请及时关注考必过网站,小编会第一时间发布考试最新消息,大家考试加油!

小讯
上一篇 2025-02-15 09:19
下一篇 2025-03-23 19:06

相关推荐

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容,请联系我们,一经查实,本站将立刻删除。
如需转载请保留出处:https://51itzy.com/kjqy/112830.html