Edit File: FileHandler.inc
<?php /** * Author: Shlomi * Date: Sep 6, 2017 12:29:07 PM * Founder: Shlomi Bazak (SBZsoft) (c) * File: FileHandler.inc * * Description: * */ namespace B2\FS; use B2\Exceptions\UnableToOpenFileException; use B2\Exceptions\NotFoundException; use B2\Exceptions\FileException; use B2\Entities\MimeTypes; // No direct access. defined("__NSS__") or die("Restrected Access."); class FileHandler { private $_stat; private $_fileName; protected $_handle; private $_tmp_handle; /** * * @param string $path * @throws NotFoundException * @throws UnableToOpenFileException */ public function __construct($path){ if(!file_exists($path)) throw new NotFoundException("File $path not found."); $this->_fileName = $path; $this->_handle = fopen($this->_fileName, "r"); if(!$this->_handle) throw new UnableToOpenFileException("File $path not readbale."); $this->_stat = null; } public function __destruct(){ if($this->_tmp_handle && is_resource($this->_tmp_handle)) { fclose($this->_tmp_handle); } @fclose($this->_handle); } public function getFileHandle(){ if($this->_tmp_handle && is_resource($this->_tmp_handle)) { fclose($this->_tmp_handle); } return $this->_tmp_handle = fopen($this->_fileName, "r+"); } public function getStat(){ if(!$this->_stat) $this->_stat = fstat($this->_handle); return $this->_stat; } public function getCursor(){ return ftell($this->_handle); } public function getName(){ return $this->_fileName; } private function _seek($offset, $whence){ if( 0 != fseek($this->_handle, $offset, $whence) ) throw new FileException("Unable to Seek to offset $offset on file {$this->_fileName}"); } public function seekCur($offset) { $this->_seek($offset, SEEK_CUR); } public function seekSet($offset) { $this->_seek($offset, SEEK_SET); } public function seekEnd($offset) { $this->_seek($offset, SEEK_END); } public function rewind() { return $this->seekSet(0); } public function eof() { return $this->getCursor() >= $this->getSize(); } public function getMimeType(){ return MimeTypes::getType($this->_fileName); } public function getConetnType(){ return $this->getMimeType(); } /** * Get device number */ public function getDevice(){ return $this->getStat()['dev']; } /** * Get inode number * On Windows this will always be 0. */ public function getInode(){ return $this->getStat()['ino']; } /** * Get inode protection mode */ public function getInodeMode(){ return $this->getStat()['mode']; } /** * Get number of links */ public function getLinkCount(){ return $this->getStat()['nlink']; } /** * Get userid of owner * On Windows this will always be 0. */ public function getUid(){ return $this->getStat()['uid']; } /** * Get groupid of owner * On Windows this will always be 0. */ public function getGid(){ return $this->getStat()['gid']; } /** * Get device type, if inode device */ public function getDeviceType(){ return $this->getStat()['rdev']; } /** * Get size in bytes */ public function getSize(){ return $this->getStat()['size']; } /** * Get time of last access (Unix timestamp) */ public function getAccessTime(){ return $this->getStat()['atime']; } /** * Get time of last modification (Unix timestamp) */ public function getModificationTime(){ return $this->getStat()['mtime']; } /** * Get time of last inode change (Unix timestamp) */ public function getCreationTime(){ return $this->getStat()['ctime']; } /** * Get blocksize of filesystem IO * Only valid on systems supporting the st_blksize type - other systems (e.g. Windows) return -1. */ public function getBlockSize(){ return $this->getStat()['blksize']; } /** * Get number of 512-byte blocks allocated * Only valid on systems supporting the st_blksize type - other systems (e.g. Windows) return -1. */ public function getBlocks(){ return $this->getStat()['blocks']; } }