123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367 |
- <?php
- class FilesizeHelper {
- private $path;
-
- private $byteSize;
-
- private $isWindows;
-
- public function __construct() {
-
- $this->isWindows = (strtoupper(substr(PHP_OS, 0, 3)) == 'WIN');
- }
-
- public function getFileSize($file, $formatted = true) {
-
- $this->path = $file;
- if (strpos($this->path, "http") === 0) {
- $url = $this->path;
- $this->byteSize = $this->_url_exists($url);
- if (empty($this->byteSize)) {
- return 0;
- }
- } else {
-
- $rs = $this->__checkFilePath();
- if (!$rs) {
- return 0;
- }
-
- $this->byteSize = (float)$this->__getByteSize();
- }
-
- if (!$this->byteSize) {
- if (!$formatted) {
- return 0;
- }
-
- $blank_size = $this->__formatFileSize();
- return array(
- 0,
- $blank_size[0],
- $blank_size[1]
- );
- }
-
- if (!$formatted) {
- return $this->byteSize;
- }
-
- return $this->__formatFileSize();
- }
-
- private function __formatFileSize() {
-
- $_size = $this->byteSize;
- if (!$_size || $_size < 0) {
- return array(
- 0,
- '0 B',
- array(
- 0,
- 'B'
- )
- );
- }
-
- if ($_size <= 1024) {
- return array(
- 0,
- '1 KB',
- array(
- 1,
- 'KB'
- )
- );
- }
-
- $size_units = Array(
- 'B',
- 'KB',
- 'MB',
- 'GB',
- 'TB',
- 'PB',
- 'EB'
- );
-
- $unit = $size_units[0];
-
- for ($i = 1; ($i < count($size_units) && $_size >= 1024); $i++) {
- $_size = $_size / 1024;
- $unit = $size_units[$i];
- }
-
- $round = 2;
-
- if ($unit == 'KB') {
- $round = 0;
- }
-
- $formatted = round((float)$_size, $round);
-
- return array(
- $this->byteSize,
- $formatted." ".$unit,
- array(
- $formatted,
- $unit
- )
- );
- }
-
- private function __checkFilePath() {
- clearstatcache();
- if (!file_exists($this->path)) {
- return false;
- }
- return true;
- }
-
- private function _url_exists($url) {
- $ch = curl_init();
- curl_setopt($ch, CURLOPT_URL, $url);
-
- curl_setopt($ch, CURLOPT_NOBODY, 1);
-
- curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 3);
- curl_setopt($ch, CURLOPT_TIMEOUT, 3);
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
-
- curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
- curl_exec($ch);
- $http = curl_getinfo($ch);
- curl_close($ch);
- if ($http['http_code'] == 200) {
- return $http['download_content_length'];
- }
- return 0;
- }
-
- private function __getByteSize() {
-
- $bytesize = @filesize($this->path);
- if (false !== $bytesize && $bytesize >= 0) {
- return $bytesize;
- }
-
- $bytesize = $this->__useCurl();
- if ($bytesize) {
- return $bytesize;
- }
-
- $bytesize = $this->__useNativeSeek();
- if ($bytesize) {
- return $bytesize;
- }
-
- $bytesize = $this->__useCom();
- if ($bytesize) {
- return $bytesize;
- }
-
- $bytesize = $this->__useExec();
- if ($bytesize) {
- return $bytesize;
- }
-
- throw new Exception("Unable to get the file size for the file ".$this->path."!");
- }
-
- private function __useCurl() {
-
- 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('/Content-Length: (\d+)/', $data, $matches)) {
- return (string)$matches[1];
- }
- }
-
- private function __useNativeSeek() {
-
- $fp = @fopen($this->path, "rb");
-
- if (!$fp) {
- return false;
- }
- flock($fp, LOCK_SH);
-
- $res = fseek($fp, 0, SEEK_END);
- if ($res === 0) {
-
- $pos = ftell($fp);
- flock($fp, LOCK_UN);
- fclose($fp);
-
-
- if ($pos >= 0) {
- return (string)$pos;
- } else {
- return sprintf("%u", $pos);
- }
- } else {
- flock($fp, LOCK_UN);
- fclose($fp);
- return false;
- }
- }
-
- private function __useCom() {
- if (!$this->isWindows || !class_exists("COM")) {
- return false;
- }
-
- $fsobj = new COM('Scripting.FileSystemObject');
- if (dirname($this->path) == '.') {
- $this->path = ((substr(getcwd(), -1) == DIRECTORY_SEPARATOR)
- ? getcwd().basename($this->path)
- : getcwd().DIRECTORY_SEPARATOR.basename(
- $this->path
- ));
- }
-
- $f = $fsobj->GetFile($this->path);
-
- return (string)$f->Size;
- }
-
- private function __useExec() {
-
- if (!function_exists("exec")) {
- return false;
- }
-
- $escapedPath = escapeshellarg($this->path);
-
- if ($this->isWindows) {
-
- $size = trim(exec("for %F in ($escapedPath) do @echo %~zF"));
- }
- else {
-
- $size = trim(exec("stat -c%s $escapedPath"));
- }
-
- if ($size && ctype_digit($size)) {
- return (string)$size;
- }
-
- return false;
- }
- }
|