| Conditions | 5 |
| Paths | 5 |
| Total Lines | 33 |
| Code Lines | 17 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 3 | ||
| Bugs | 0 | Features | 0 |
| 1 | <?php |
||
| 18 | public function getFileSize($path) |
||
| 19 | { |
||
| 20 | // This should work for large files on 64bit platforms and for small files everywhere |
||
| 21 | $fp = fopen($path, "rb"); |
||
| 22 | if (!$fp) { |
||
| 23 | throw new Exception("Cannot open specified file for reading."); |
||
| 24 | } |
||
| 25 | |||
| 26 | $flockResult = flock($fp, LOCK_SH); |
||
| 27 | $seekResult = fseek($fp, 0, SEEK_END); |
||
| 28 | $position = ftell($fp); |
||
| 29 | flock($fp, LOCK_UN); |
||
| 30 | fclose($fp); |
||
| 31 | |||
| 32 | if($flockResult === false) { |
||
| 33 | throw new Exception("Couldn't get file lock. Operation abandoned."); |
||
| 34 | } |
||
| 35 | |||
| 36 | if($seekResult !== 0) { |
||
| 37 | throw new Exception("Seeking to end of file failed"); |
||
| 38 | } |
||
| 39 | |||
| 40 | if($position === false) { |
||
| 41 | throw new Exception("Cannot determine position in file. ftell() failed."); |
||
| 42 | } |
||
| 43 | |||
| 44 | // PHP uses internally (in C) UNSIGNED integer for file size. |
||
| 45 | // PHP uses signed implicitly |
||
| 46 | // convert signed (max val +2^31) -> unsigned integer will extend range for 32-bit to (+2^32) |
||
| 47 | return BigInteger::of( |
||
| 48 | sprintf("%u", $position) |
||
| 49 | ); |
||
| 50 | } |
||
| 51 | } |