| 1 | <?php |
| 2 | |
| 3 | namespace wcf\system\io; |
| 4 | |
| 5 | use wcf\system\exception\SystemException; |
| 6 | |
| 7 | /** |
| 8 | * The File class handles all file operations. |
| 9 | * |
| 10 | * Example: |
| 11 | * using php functions: |
| 12 | * $fp = fopen('filename', 'wb'); |
| 13 | * fwrite($fp, '...'); |
| 14 | * fclose($fp); |
| 15 | * |
| 16 | * using this class: |
| 17 | * $file = new File('filename'); |
| 18 | * $file->write('...'); |
| 19 | * $file->close(); |
| 20 | * |
| 21 | * @author Marcel Werk |
| 22 | * @copyright 2001-2019 WoltLab GmbH |
| 23 | * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php> |
| 24 | * |
| 25 | * @method bool close() |
| 26 | * @method bool eof() |
| 27 | * @method int filesize() |
| 28 | * @method string gets($length = null) |
| 29 | * @method resource open($mode, $use_include_path = false, $context = null) |
| 30 | * @method int puts($string, $length = null) alias of `write` |
| 31 | * @method string read($length) |
| 32 | * @method int seek($offset, $whence = SEEK_SET) |
| 33 | * @method array stat() |
| 34 | * @method int tell() |
| 35 | * @method bool touch($time = 0, $atime = 0) note: default value of `$time` actually is `time()` |
| 36 | * @method int write($string, $length = null) |
| 37 | */ |
| 38 | class File |
| 39 | { |
| 40 | /** |
| 41 | * file pointer resource |
| 42 | * @var resource |
| 43 | */ |
| 44 | protected $resource; |
| 45 | |
| 46 | /** |
| 47 | * filename |
| 48 | * @var string |
| 49 | */ |
| 50 | protected $filename = ''; |
| 51 | |
| 52 | /** |
| 53 | * Opens a new file. |
| 54 | * |
| 55 | * @param string $filename |
| 56 | * @param string $mode |
| 57 | * @param array $options |
| 58 | * @throws SystemException |
| 59 | */ |
| 60 | public function __construct($filename, $mode = 'wb', $options = []) |
| 61 | { |
| 62 | $this->filename = $filename; |
| 63 | if (!empty($options)) { |
| 64 | $context = \stream_context_create($options); |
| 65 | $this->resource = \fopen($filename, $mode, false, $context); |
| 66 | } else { |
| 67 | $this->resource = \fopen($filename, $mode); |
| 68 | } |
| 69 | if ($this->resource === false) { |
| 70 | throw new SystemException('Can not open file ' . $filename); |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | /** |
| 75 | * Calls the specified function on the open file. |
| 76 | * Do not call this function directly. Use $file->write('') instead. |
| 77 | * |
| 78 | * @param string $function |
| 79 | * @param array $arguments |
| 80 | * @return mixed |
| 81 | * @throws SystemException |
| 82 | */ |
| 83 | public function __call($function, $arguments) |
| 84 | { |
| 85 | if (\function_exists('f' . $function)) { |
| 86 | \array_unshift($arguments, $this->resource); |
| 87 | |
| 88 | return \call_user_func_array('f' . $function, $arguments); |
| 89 | } elseif (\function_exists($function)) { |
| 90 | \array_unshift($arguments, $this->filename); |
| 91 | |
| 92 | return \call_user_func_array($function, $arguments); |
| 93 | } else { |
| 94 | throw new SystemException('Can not call file method ' . $function); |
| 95 | } |
| 96 | } |
| 97 | } |