Edit File: FileChunkStream.inc
<?php /** * Author: Shlomi * Date: Jun 11, 2019 2:09:18 PM * Founder: Shlomi Bazak (SBZsoft) (c) * File: FileChunkStream.inc * * Description: * */ namespace B2\FS; use B2\Exceptions\FileException; // No direct access. defined("__NSS__") or die("Restrected Access."); class FileChunkStream { const BB_CHUNK_FILE_PREFIX = "bb_chunk_"; private $_stream; private $_fileReader; private $_chunkSize; private $_size; private $_sha1; private $_tmpFileName; public function __construct(FileReader $reader){ $this->_fileReader = $reader; $this->_stream = @fopen($this->_tmpFileName=tempnam(sys_get_temp_dir(), self::BB_CHUNK_FILE_PREFIX), 'r+'); if(false === $this->_stream) throw new FileException("Unable to create temp chunk file."); $this->_chunkSize = $reader->getChunkSize(); $this->_size = 0; $this->_sha1 = ""; $this->_prepare(); } private function _prepare(){ $len = 1000000; // local copy while($this->_chunkSize > 0 && !$this->_fileReader->eof()) { if($this->_chunkSize <= $len){ $len = $this->_chunkSize; } $this->_chunkSize -= $len; if(false === fwrite($this->_stream, $this->_fileReader->read($len))) throw new FileException("Can't write to temp chunk file."); } fflush($this->_stream); $this->_size = filesize($this->_tmpFileName); $this->_sha1 = sha1_file($this->_tmpFileName); } public function getData(){ if($this->_stream && is_resource($this->_stream)) fclose($this->_stream); return $this->_stream = fopen($this->_tmpFileName, "r"); } public function getSize(){ return $this->_size; } public function getHash(){ return $this->_sha1; } public function __destruct(){ if($this->_stream && is_resource($this->_stream)) fclose($this->_stream); if(file_exists($this->_tmpFileName)) @unlink($this->_tmpFileName); } }