3 namespace wcf\system\io
;
5 use wcf\system\exception\SystemException
;
8 * The File class handles all file operations.
11 * using php functions:
12 * $fp = fopen('filename', 'wb');
17 * $file = new File('filename');
18 * $file->write('...');
22 * @copyright 2001-2019 WoltLab GmbH
23 * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
25 * @method bool close()
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()
35 * @method bool touch($time = 0, $atime = 0) note: default value of `$time` actually is `time()`
36 * @method int write($string, $length = null)
41 * file pointer resource
50 protected $filename = '';
55 * @param string $filename
57 * @param array $options
58 * @throws SystemException
60 public function __construct($filename, $mode = 'wb', $options = [])
62 $this->filename
= $filename;
63 if (!empty($options)) {
64 $context = \
stream_context_create($options);
65 $this->resource = \fopen
($filename, $mode, false, $context);
67 $this->resource = \fopen
($filename, $mode);
69 if ($this->resource === false) {
70 throw new SystemException('Can not open file ' . $filename);
75 * Calls the specified function on the open file.
76 * Do not call this function directly. Use $file->write('') instead.
78 * @param string $function
79 * @param array $arguments
81 * @throws SystemException
83 public function __call($function, $arguments)
85 if (\function_exists
('f' . $function)) {
86 \array_unshift
($arguments, $this->resource);
88 return \
call_user_func_array('f' . $function, $arguments);
89 } elseif (\function_exists
($function)) {
90 \array_unshift
($arguments, $this->filename
);
92 return \
call_user_func_array($function, $arguments);
94 throw new SystemException('Can not call file method ' . $function);