NativeReadDriver   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 27
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Importance

Changes 2
Bugs 1 Features 0
Metric Value
wmc 3
lcom 0
cbo 2
dl 0
loc 27
c 2
b 1
f 0
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
A getFileSize() 0 19 3
1
<?php
2
3
namespace BigFileTools\Driver;
4
5
use Brick\Math\BigInteger;
6
7
class NativeReadDriver implements ISizeDriver
8
{
9
	/**
10
	 * Returns file size by reading whole files and counting read bites
11
	 * @link http://stackoverflow.com/questions/5501451/php-x86-how-to-get-filesize-of-2gb-file-without-external-program/5504829#5504829
12
	 * @inheritdoc
13
	 */
14
	public function getFileSize($path)
15
	{
16
		$fp = fopen($path, "rb");
17
		if (!$fp) {
18
			throw new Exception("Cannot read from file.");
19
		}
20
21
		flock($fp, LOCK_SH);
22
		rewind($fp);
23
		$fileSize = BigInteger::zero();
24
		$chunkSize = 1024 * 1024;
25
		while (!feof($fp)) {
26
			$readBytes = strlen(fread($fp, $chunkSize));
27
			$fileSize = $fileSize->plus($readBytes);
28
		}
29
		flock($fp, LOCK_UN);
30
		fclose($fp);
31
		return $fileSize;
32
	}
33
}