ISGArchiveDelegate.php
3 years ago
SGBGArchive.php
3 years ago
SGBGArchiveCdr.php
3 years ago
SGBGArchiveHelper.php
3 years ago
SGBGCache.php
3 years ago
SGBGCacheableFile.php
3 years ago
SGBGDirectoryTreeFile.php
3 years ago
SGBGFile.php
3 years ago
SGBGFileHelper.php
3 years ago
SGBGJsonFile.php
3 years ago
SGBGLog.php
3 years ago
SGBGReloader.php
3 years ago
SGBGStateFile.php
3 years ago
SGBGTask.php
3 years ago
SGBGFile.php
228 lines
| 1 | <?php |
| 2 | /* |
| 3 | @ class File |
| 4 | @ version 1.0.4 |
| 5 | @ updated 12/02/2021 |
| 6 | */ |
| 7 | |
| 8 | class SGBGFile |
| 9 | { |
| 10 | private $path = ''; |
| 11 | private $handle = null; |
| 12 | private $splFileObject = null; |
| 13 | |
| 14 | /** |
| 15 | * |
| 16 | * Constructor. |
| 17 | * After the constructor call, the file will be created if it doesn't exist. |
| 18 | * |
| 19 | * @param string $path absolute file path |
| 20 | * @return null |
| 21 | */ |
| 22 | public function __construct($path) |
| 23 | { |
| 24 | if (!self::isPathWritable($path)) { |
| 25 | throw new Exception('File path is not writable. ---'.$path); |
| 26 | } |
| 27 | |
| 28 | if (is_dir($path)) { |
| 29 | throw new Exception('Specified path is a directory. ---'.$path); |
| 30 | } |
| 31 | |
| 32 | $this->path = $path; |
| 33 | } |
| 34 | |
| 35 | /** |
| 36 | * |
| 37 | * Destructor. |
| 38 | * |
| 39 | * @return null |
| 40 | */ |
| 41 | public function __destruct() |
| 42 | { |
| 43 | $this->close(); |
| 44 | } |
| 45 | |
| 46 | /** |
| 47 | * |
| 48 | * Get the file path. |
| 49 | * |
| 50 | * @return string the file path |
| 51 | */ |
| 52 | public function getPath() |
| 53 | { |
| 54 | return $this->path; |
| 55 | } |
| 56 | |
| 57 | /** |
| 58 | * |
| 59 | * Check whether the provided path is writable. |
| 60 | * |
| 61 | * @return string the file path |
| 62 | */ |
| 63 | public static function isPathWritable($path) |
| 64 | { |
| 65 | if (empty($path) || !is_string($path)) { |
| 66 | return false; |
| 67 | } |
| 68 | |
| 69 | //if path is a file and it doesn't exist, check parent folder |
| 70 | if (substr($path, -1) != '/' && !file_exists($path)) { |
| 71 | $path = dirname($path); |
| 72 | } |
| 73 | |
| 74 | return is_writable($path); |
| 75 | } |
| 76 | |
| 77 | /** |
| 78 | * |
| 79 | * Open the file for the specified operations. |
| 80 | * |
| 81 | * @return null |
| 82 | */ |
| 83 | public function open($mode) |
| 84 | { |
| 85 | $this->handle = @fopen($this->getPath(), $mode); |
| 86 | if (!is_resource($this->handle)) { |
| 87 | throw new Exception('Could not open file: '.$this->getPath()); |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | /** |
| 92 | * |
| 93 | * Close file. |
| 94 | * |
| 95 | * @return null |
| 96 | */ |
| 97 | public function close() |
| 98 | { |
| 99 | if (is_resource($this->handle)) { |
| 100 | @fclose($this->handle); |
| 101 | $this->handle = null; |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | /** |
| 106 | * |
| 107 | * Write data into the file. |
| 108 | * |
| 109 | * @param string $data the data to write |
| 110 | * @return int the number of bytes written to file |
| 111 | */ |
| 112 | public function write($data) |
| 113 | { |
| 114 | if (!is_resource($this->handle)) { |
| 115 | return 0; |
| 116 | } |
| 117 | |
| 118 | return @fwrite($this->handle, $data); |
| 119 | } |
| 120 | |
| 121 | /** |
| 122 | * |
| 123 | * Read file contents up to length bytes. |
| 124 | * |
| 125 | * @param int $length the number of bytes to read, if null, read until reaching the end of file |
| 126 | * @return string|false the read string or false on failure |
| 127 | */ |
| 128 | public function read($length = null) |
| 129 | { |
| 130 | if (!is_resource($this->handle)) { |
| 131 | return false; |
| 132 | } |
| 133 | |
| 134 | if ($length === null) { |
| 135 | $length = $this->getSize(); |
| 136 | } |
| 137 | |
| 138 | return @fread($this->handle, $length); |
| 139 | } |
| 140 | |
| 141 | /** |
| 142 | * |
| 143 | * Get the current position of the file read/write pointer. |
| 144 | * |
| 145 | * @return int|false the position of the file pointer or false on failure |
| 146 | */ |
| 147 | public function tell() |
| 148 | { |
| 149 | return @ftell($this->handle); |
| 150 | } |
| 151 | |
| 152 | /** |
| 153 | * |
| 154 | * Change the file position indicator. |
| 155 | * |
| 156 | * @param int $offset position to seek |
| 157 | * @param int $whence SEEK_SET, SEEK_CUR or SEEK_END |
| 158 | * @return bool upon success, returns true; otherwise, false |
| 159 | */ |
| 160 | public function seek($offset, $whence = SEEK_SET) |
| 161 | { |
| 162 | if (@fseek($this->handle, $offset, $whence) === 0) { |
| 163 | return true; |
| 164 | } |
| 165 | |
| 166 | return false; |
| 167 | } |
| 168 | |
| 169 | /** |
| 170 | * |
| 171 | * Check whether we have reached end of file |
| 172 | * |
| 173 | * @return bool |
| 174 | */ |
| 175 | public function eof() |
| 176 | { |
| 177 | return feof($this->handle); |
| 178 | } |
| 179 | |
| 180 | /** |
| 181 | * |
| 182 | * Get file size. |
| 183 | * |
| 184 | * @return int|false the size of the file in bytes, or false in case of an error |
| 185 | */ |
| 186 | public function getSize() |
| 187 | { |
| 188 | return @filesize($this->getPath()); |
| 189 | } |
| 190 | |
| 191 | /** |
| 192 | * |
| 193 | * Delete file. |
| 194 | * |
| 195 | * @return null |
| 196 | */ |
| 197 | public function remove() |
| 198 | { |
| 199 | @unlink($this->getPath()); |
| 200 | } |
| 201 | |
| 202 | /** |
| 203 | * |
| 204 | * Check whether file exists. |
| 205 | * |
| 206 | * @return bool true if the file exists; false otherwise |
| 207 | */ |
| 208 | public function exists() |
| 209 | { |
| 210 | return @file_exists($this->getPath()); |
| 211 | } |
| 212 | |
| 213 | /** |
| 214 | * |
| 215 | * Get an instance of SplFileObject for the current file. |
| 216 | * |
| 217 | * @return SplFileObject instance of SplFileObject |
| 218 | */ |
| 219 | public function getSplFileObject() |
| 220 | { |
| 221 | if (!$this->splFileObject) { |
| 222 | $this->splFileObject = new \SplFileObject($this->getPath()); |
| 223 | } |
| 224 | |
| 225 | return $this->splFileObject; |
| 226 | } |
| 227 | } |
| 228 |