热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

php操作文件大小辅助类-PHP源码

php操作文件大小辅助类
这个类可以判断文件的大小,即使大于2GB,它可以使用不同的方法来确定一个大的文件。

getFileSize($file);
echo"
";
var_dump($filesize);
isWindows = (strtoupper(substr(PHP_OS, 0, 3)) == 'WIN');
}

/**
 * Gets the size of the specified file
*
 * @accesspublic
 * @paramstringThe file path
 * @paramboolWhether to format the file size in KB, MB, GB, TB
 * @return mixed
*/
public function getFileSize($file, $formatted = true) {

// Set the path of the file
$this->path = $file;

// Check for a valid file path
$this->__checkFilePath();

// Get the file size in bytes
$this->byteSize = (float) $this->__getByteSize();

// If failed to get the file size or the file size is zero, return a blank result
if (!$this->byteSize) {
if (!$formatted) {
return 0;
}

// Return a blank array
$blank_size = $this->__formatFileSize();
return array(0, $blank_size[0], $blank_size[1]);
}

// Return the bytesize if no formatting is needed
if (!$formatted) {
return $this->byteSize;
}

// Return an array containing the file size information
return $this->__formatFileSize();
}

/**
 * Formats the file size in KB, MB, GB, TB units
*
 * @accessprivate
 * @param void
 * @return arrayReturn arry containing the file size information
*/
private function __formatFileSize() {

// If the file size is zero return a blank result
$_size = $this->byteSize;
if (!$_size || $_size <0) {
return array(0, &#39;0 B&#39;, array(0, &#39;B&#39;));
}

// If the file size is smaller than 1KB
if ($_size <= 1024) {
return array(0, &#39;1 KB&#39;, array(1, &#39;KB&#39;));
}

// Set an array of all file size units
$size_units = Array(&#39;B&#39;, &#39;KB&#39;, &#39;MB&#39;, &#39;GB&#39;, &#39;TB&#39;, &#39;PB&#39;, &#39;EB&#39;);
// Set the initial unit to Bytes
$unit = $size_units[0];

// Loop through all file size units
for ($i = 1; ($i = 1024); $i++) {
$_size = $_size / 1024;
$unit = $size_units[$i];
}

// Set the number of digits after the decimal place in the resulted file size
$round = 2;

// If the file size is in KiloByte we do not need any decimal numbers
if ($unit == &#39;KB&#39;) {
$round = 0;
}

// Round the file size
$formatted = round((float) $_size, $round);

// Return the file size data
return array($this->byteSize, $formatted ."". $unit, array($formatted, $unit));
}

/**
 * Chek if the file is exist
*
 * @accessprivate
 * @param void
 * @return void
*/
private function __checkFilePath() {

clearstatcache();
if (!file_exists($this->path)) {
throw new Exception("File not found at $this->path");
}
}

/**
 * Gets the size of the specified file in bytes
*
 * @accessprivate
 * @param void
 * @return stringThe file size in bytes
*/
private function __getByteSize() {

// Try the php native filesize() function.
$bytesize = @filesize($this->path);
if (false !== $bytesize && $bytesize >= 0) {
return $bytesize;
}

// If filesize() fails with larger files, try to get the size using curl module.
$bytesize = $this->__useCurl();
if ($bytesize) {
return $bytesize;
}

// If curl fails to get the file size try using the php native seek function.
$bytesize = $this->__useNativeSeek();
if ($bytesize) {
return $bytesize;
}

// If the native seek fails to get the file size and we are on windows try using Windows COM interface
$bytesize = $this->__useCom();
if ($bytesize) {
return $bytesize;
}

// If all the above methods failed to get the file size try using external program (exec() function needed)
$bytesize = $this->__useExec();
if ($bytesize) {
return $bytesize;
}

// Unable to get the file size in bytes
throw new Exception("Unable to get the file size for the file". $this->path ."!");
}

/**
 * Gets the file size using curl module
*
 * @accessprivate
 * @param void
 * @returnmixedThe file size as string or false when fail or cUrl module not available
 * @seehttp://www.php.net/manual/en/function.filesize.php#100434
*/
private function __useCurl() {

// If the curl module is not available return false
if (!function_exists("curl_init")) {
return false;
}

$ch = curl_init("file://". $this->path);
curl_setopt($ch, CURLOPT_NOBODY, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, true);
$data = curl_exec($ch);
curl_close($ch);

if ($data !== false && preg_match(&#39;/Content-Length: (d+)/&#39;, $data, $matches)) {
return (string) $matches[1];
}
}

/**
 * Gets the file size by using native fseek function
*
 * @accessprivate
 * @param void
 * @returnmixedThe file size as string or false when fail
 * @seehttp://www.php.net/manual/en/function.filesize.php#79023
 * @seehttp://www.php.net/manual/en/function.filesize.php#102135
*/
private function __useNativeSeek() {

// This should work for large files on 64bit platforms and for small files every where
$fp = @fopen($this->path,"rb");

// If failed to open the file return false
if (!$fp) {
return false;
}

flock($fp, LOCK_SH);

// Seeks past the end-of-file
$res = fseek($fp, 0, SEEK_END);
if ($res === 0) {
// Get the current position of the file pointer
$pos = ftell($fp);

flock($fp, LOCK_UN);
fclose($fp);

// $pos will be positive int if file is <2GB
// if is >2GB <4GB it will be negative number
if ($pos >= 0) {
return (string) $pos;
}
else {
return sprintf("%u", $pos);
}
}
else {
flock($fp, LOCK_UN);
fclose($fp);
return false;
}
}

/**
 * Gets the file size by using Windows COM interface
*
 * @accessprivate
 * @param void
 * @returnmixedThe file size as string or false when fail or COM not available
*/
private function __useCom() {

if (!$this->isWindows || !class_exists("COM")) {
return false;
}

// Use the Windows COM interface
$fsobj = new COM(&#39;Scripting.FileSystemObject&#39;);

if (dirname($this->path) == &#39;.&#39;) {
$this->path = ((substr(getcwd(), -1) == DIRECTORY_SEPARATOR) ? getcwd() . basename($this->path) : getcwd() . DIRECTORY_SEPARATOR . basename($this->path));
}

// Get the file data
$f = $fsobj->GetFile($this->path);

// Convert to string
return (string) $f->Size;
}

/**
 * Gets the file size by using external program (exec needed)
*
 * @accessprivate
 * @param void
 * @return mixedThe file size as string or false when fail or or exec is disabled
*/
private function __useExec() {

// If exeec is disable return false
if (!function_exists("exec")) {
return false;
}

//Escape the file path string to be used as a shell argument
$escapedPath = escapeshellarg($this->path);

// If we are on Windows
if ($this->isWindows) {
// Try using the NT substition modifier %~z
$size = trim(exec("for %F in ($escapedPath) do @echo %~zF"));
}

// If other OS (*nix and MacOS)
else {
// If the platform is not Windows, use the stat command (should work for *nix and MacOS)
$size = trim(exec("stat -c%s $escapedPath"));
}

// If the return is not blank, not zero, and is number
if ($size && ctype_digit($size)) {
return (string) $size;
}

// An error has occured
return false;
}
}

推荐阅读
  • scrcpy通过adb调试的方式来将手机屏幕投到电脑上,并可以通过电脑控制您的Android设备。它可以通过USB连接,也可以通过Wifi连接(类似于隔空投屏),而且不需要任何ro ... [详细]
  • 2016 linux发行版排行_灵越7590 安装 linux (manjarognome)
    RT之前做了一次灵越7590黑苹果炒作业的文章,希望能够分享给更多不想折腾的人。kawauso:教你如何给灵越7590黑苹果抄作业​zhuanlan.z ... [详细]
  • Java验证码——kaptcha的使用配置及样式
    本文介绍了如何使用kaptcha库来实现Java验证码的配置和样式设置,包括pom.xml的依赖配置和web.xml中servlet的配置。 ... [详细]
  • 安卓及谷歌官网不容易上,在此整理好下载地址,这样就可以直接用迅雷下载了。Eclipse最新Mars版Eclipse(暂时还没被墙)Mac版:http:www.eclipse.org ... [详细]
  • steam,2,7,2,已经,发布 ... [详细]
  • Skywalking系列博客1安装单机版 Skywalking的快速安装方法
    本文介绍了如何快速安装单机版的Skywalking,包括下载、环境需求和端口检查等步骤。同时提供了百度盘下载地址和查询端口是否被占用的命令。 ... [详细]
  • 本文讨论了如何优化解决hdu 1003 java题目的动态规划方法,通过分析加法规则和最大和的性质,提出了一种优化的思路。具体方法是,当从1加到n为负时,即sum(1,n)sum(n,s),可以继续加法计算。同时,还考虑了两种特殊情况:都是负数的情况和有0的情况。最后,通过使用Scanner类来获取输入数据。 ... [详细]
  • Windows下配置PHP5.6的方法及注意事项
    本文介绍了在Windows系统下配置PHP5.6的步骤及注意事项,包括下载PHP5.6、解压并配置IIS、添加模块映射、测试等。同时提供了一些常见问题的解决方法,如下载缺失的msvcr110.dll文件等。通过本文的指导,读者可以轻松地在Windows系统下配置PHP5.6,并解决一些常见的配置问题。 ... [详细]
  • Metasploit攻击渗透实践
    本文介绍了Metasploit攻击渗透实践的内容和要求,包括主动攻击、针对浏览器和客户端的攻击,以及成功应用辅助模块的实践过程。其中涉及使用Hydra在不知道密码的情况下攻击metsploit2靶机获取密码,以及攻击浏览器中的tomcat服务的具体步骤。同时还讲解了爆破密码的方法和设置攻击目标主机的相关参数。 ... [详细]
  • 本文介绍了C#中数据集DataSet对象的使用及相关方法详解,包括DataSet对象的概述、与数据关系对象的互联、Rows集合和Columns集合的组成,以及DataSet对象常用的方法之一——Merge方法的使用。通过本文的阅读,读者可以了解到DataSet对象在C#中的重要性和使用方法。 ... [详细]
  • 本文介绍了OC学习笔记中的@property和@synthesize,包括属性的定义和合成的使用方法。通过示例代码详细讲解了@property和@synthesize的作用和用法。 ... [详细]
  • Mac OS 升级到11.2.2 Eclipse打不开了,报错Failed to create the Java Virtual Machine
    本文介绍了在Mac OS升级到11.2.2版本后,使用Eclipse打开时出现报错Failed to create the Java Virtual Machine的问题,并提供了解决方法。 ... [详细]
  • 在说Hibernate映射前,我们先来了解下对象关系映射ORM。ORM的实现思想就是将关系数据库中表的数据映射成对象,以对象的形式展现。这样开发人员就可以把对数据库的操作转化为对 ... [详细]
  • macOS10.12安装win10系统教程,实现双系统安装
    本文介绍了如何在macOS10.12系统上安装win10系统,实现双系统的安装。通过使用Boot Camp助理,选取win10系统镜像并分配系统容量,然后进行安装。安装完win10系统后,安装驱动并重启系统即可完成双系统的安装。 ... [详细]
  • PL2303HXD电路图(USB转UART)介绍及应用
    本文介绍了PL2303HXD电路图(USB转UART)的特性和应用,该电路图可以实现RS232和USB信号的转换,方便嵌入到手持设备中。PL2303HXD作为USB/RS232双向转换器,可以将USB数据转换为RS232信息流格式发送给外设,并将RS232外设的数据转换为USB数据格式传送回主机。通过利用USB块传输模式和自动流量控制,PL2303HXD能够实现更高的数据传输吞吐量比传统的UART端口。 ... [详细]
author-avatar
淡而有味调_740
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有