NativeReadDriver::getFileSize()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 19
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 1 Features 0
Metric Value
dl 0
loc 19
c 2
b 1
f 0
rs 9.4285
cc 3
eloc 14
nc 3
nop 1
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
}