Edit File: FileReader.inc
<?php /** * Author: Shlomi * Date: Sep 6, 2017 10:43:44 AM * Founder: Shlomi Bazak (SBZsoft) (c) * File: FileInfo.inc * * Description: * */ namespace B2\FS; use B2\Exceptions\FileException; use B2\Exceptions\B2Exception; // No direct access. defined("__NSS__") or die("Restrected Access."); class FileReader extends FileHandler{ const CHUNK_SIZE = 1000000; // 1MB private $_chunkSize; /** * Create a new instance of file reader. * @param string $path path to the file to read. */ public function __construct($path){ parent::__construct($path); $this->_chunkSize = self::CHUNK_SIZE; } /** * Set size of chanks to be the default read length. * @param int $size */ public function setChunkSize($size){ $size = (int)$size; if($size <= 0) throw new B2Exception("Function " . __CLASS__."::".__METHOD__ . " expects size to be positive integer. Got $size"); $this->_chunkSize = (int)$size; } public function getChunkSize() { return $this->_chunkSize; } /** * read next set of chars. * If $length is 0|null it will use the internal chunkSize. * @param int $length the max length of bytes to read. * @throws FileException if there is an error in reading. * @return string the readen data */ public function &read($length=null) { if($this->eof()) throw new FileException("Unable to read, we've hit EOF "); $length = (int)$length; if(!$length) $length = $this->_chunkSize; $bytes = fread($this->_handle, $length); if($bytes === false) throw new FileException("Unable to read from file {$this->_fileName}"); return $bytes; } /** * If we hit EoF it will return null, B2\FS\FileChunk otherwise. * @throws FileException if there is an error in reading. * @return NULL|\B2\FS\FileChunk */ public function getNextChunk(){ if($this->eof()) return null; return new FileChunk($this->read()); } public function getNextChunkStream(){ if($this->eof()) return null; return new FileChunkStream($this); } /** * Read file on the fly. * @return string */ public function getContents() { $data = ""; $lastPos = $this->getCursor(); $this->rewind(); while(!$this->eof()) { $data .= $this->read(); } $this->seekSet($lastPos); return $data; } }