Exceptions
5 days ago
apache.php
5 days ago
apcu.php
5 days ago
array.php
5 days ago
bzip2.php
5 days ago
calendar.php
5 days ago
classobj.php
5 days ago
com.php
5 days ago
cubrid.php
5 days ago
curl.php
5 days ago
datetime.php
5 days ago
dir.php
5 days ago
eio.php
5 days ago
errorfunc.php
5 days ago
exec.php
5 days ago
fileinfo.php
5 days ago
filesystem.php
5 days ago
filter.php
5 days ago
fpm.php
5 days ago
ftp.php
5 days ago
funchand.php
5 days ago
functionsList.php
5 days ago
gmp.php
5 days ago
gnupg.php
5 days ago
hash.php
5 days ago
ibase.php
5 days ago
ibmDb2.php
5 days ago
iconv.php
5 days ago
image.php
5 days ago
imap.php
5 days ago
info.php
5 days ago
ingres-ii.php
5 days ago
inotify.php
5 days ago
json.php
5 days ago
ldap.php
5 days ago
libxml.php
5 days ago
lzf.php
5 days ago
mailparse.php
5 days ago
mbstring.php
5 days ago
misc.php
5 days ago
msql.php
5 days ago
mysql.php
5 days ago
mysqli.php
5 days ago
mysqlndMs.php
5 days ago
mysqlndQc.php
5 days ago
network.php
5 days ago
oci8.php
5 days ago
opcache.php
5 days ago
openssl.php
5 days ago
outcontrol.php
5 days ago
password.php
5 days ago
pcntl.php
5 days ago
pcre.php
5 days ago
pdf.php
5 days ago
pgsql.php
5 days ago
posix.php
5 days ago
ps.php
5 days ago
pspell.php
5 days ago
readline.php
5 days ago
rpminfo.php
5 days ago
rrd.php
5 days ago
sem.php
5 days ago
session.php
5 days ago
shmop.php
5 days ago
simplexml.php
5 days ago
sockets.php
5 days ago
sodium.php
5 days ago
solr.php
5 days ago
spl.php
5 days ago
sqlsrv.php
5 days ago
ssdeep.php
5 days ago
ssh2.php
5 days ago
stream.php
5 days ago
strings.php
5 days ago
swoole.php
5 days ago
uodbc.php
5 days ago
uopz.php
5 days ago
url.php
5 days ago
var.php
5 days ago
xdiff.php
5 days ago
xml.php
5 days ago
xmlrpc.php
5 days ago
yaml.php
5 days ago
yaz.php
5 days ago
zip.php
5 days ago
zlib.php
5 days ago
filesystem.php
1408 lines
| 1 | <?php |
| 2 | |
| 3 | namespace ProfilePressVendor\Safe; |
| 4 | |
| 5 | use ProfilePressVendor\Safe\Exceptions\FilesystemException; |
| 6 | /** |
| 7 | * Attempts to change the group of the file filename |
| 8 | * to group. |
| 9 | * |
| 10 | * Only the superuser may change the group of a file arbitrarily; other users |
| 11 | * may change the group of a file to any group of which that user is a member. |
| 12 | * |
| 13 | * @param string $filename Path to the file. |
| 14 | * @param string|int $group A group name or number. |
| 15 | * @throws FilesystemException |
| 16 | * |
| 17 | */ |
| 18 | function chgrp(string $filename, $group): void |
| 19 | { |
| 20 | error_clear_last(); |
| 21 | $result = \chgrp($filename, $group); |
| 22 | if ($result === \false) { |
| 23 | throw FilesystemException::createFromPhpError(); |
| 24 | } |
| 25 | } |
| 26 | /** |
| 27 | * Attempts to change the mode of the specified file to that given in |
| 28 | * mode. |
| 29 | * |
| 30 | * @param string $filename Path to the file. |
| 31 | * @param int $mode Note that mode is not automatically |
| 32 | * assumed to be an octal value, so to ensure the expected operation, |
| 33 | * you need to prefix mode with a zero (0). |
| 34 | * Strings such as "g+w" will not work properly. |
| 35 | * |
| 36 | * |
| 37 | * |
| 38 | * |
| 39 | * ]]> |
| 40 | * |
| 41 | * |
| 42 | * |
| 43 | * The mode parameter consists of three octal |
| 44 | * number components specifying access restrictions for the owner, |
| 45 | * the user group in which the owner is in, and to everybody else in |
| 46 | * this order. One component can be computed by adding up the needed |
| 47 | * permissions for that target user base. Number 1 means that you |
| 48 | * grant execute rights, number 2 means that you make the file |
| 49 | * writeable, number 4 means that you make the file readable. Add |
| 50 | * up these numbers to specify needed rights. You can also read more |
| 51 | * about modes on Unix systems with 'man 1 chmod' |
| 52 | * and 'man 2 chmod'. |
| 53 | * |
| 54 | * |
| 55 | * |
| 56 | * |
| 57 | */ |
| 58 | function chmod(string $filename, int $mode): void |
| 59 | { |
| 60 | error_clear_last(); |
| 61 | $result = \chmod($filename, $mode); |
| 62 | if ($result === \false) { |
| 63 | throw FilesystemException::createFromPhpError(); |
| 64 | } |
| 65 | } |
| 66 | /** |
| 67 | * Attempts to change the owner of the file filename |
| 68 | * to user user. Only the superuser may change the |
| 69 | * owner of a file. |
| 70 | * |
| 71 | * @param string $filename Path to the file. |
| 72 | * @param string|int $user A user name or number. |
| 73 | * @throws FilesystemException |
| 74 | * |
| 75 | */ |
| 76 | function chown(string $filename, $user): void |
| 77 | { |
| 78 | error_clear_last(); |
| 79 | $result = \chown($filename, $user); |
| 80 | if ($result === \false) { |
| 81 | throw FilesystemException::createFromPhpError(); |
| 82 | } |
| 83 | } |
| 84 | /** |
| 85 | * Makes a copy of the file source to |
| 86 | * dest. |
| 87 | * |
| 88 | * If you wish to move a file, use the rename function. |
| 89 | * |
| 90 | * @param string $source Path to the source file. |
| 91 | * @param string $dest The destination path. If dest is a URL, the |
| 92 | * copy operation may fail if the wrapper does not support overwriting of |
| 93 | * existing files. |
| 94 | * |
| 95 | * If the destination file already exists, it will be overwritten. |
| 96 | * @param resource $context A valid context resource created with |
| 97 | * stream_context_create. |
| 98 | * @throws FilesystemException |
| 99 | * |
| 100 | */ |
| 101 | function copy(string $source, string $dest, $context = null): void |
| 102 | { |
| 103 | error_clear_last(); |
| 104 | if ($context !== null) { |
| 105 | $result = \copy($source, $dest, $context); |
| 106 | } else { |
| 107 | $result = \copy($source, $dest); |
| 108 | } |
| 109 | if ($result === \false) { |
| 110 | throw FilesystemException::createFromPhpError(); |
| 111 | } |
| 112 | } |
| 113 | /** |
| 114 | * Given a string containing a directory, this function will return the |
| 115 | * number of bytes available on the corresponding filesystem or disk |
| 116 | * partition. |
| 117 | * |
| 118 | * @param string $directory A directory of the filesystem or disk partition. |
| 119 | * |
| 120 | * Given a file name instead of a directory, the behaviour of the |
| 121 | * function is unspecified and may differ between operating systems and |
| 122 | * PHP versions. |
| 123 | * @return float Returns the number of available bytes as a float. |
| 124 | * @throws FilesystemException |
| 125 | * |
| 126 | */ |
| 127 | function disk_free_space(string $directory): float |
| 128 | { |
| 129 | error_clear_last(); |
| 130 | $result = \disk_free_space($directory); |
| 131 | if ($result === \false) { |
| 132 | throw FilesystemException::createFromPhpError(); |
| 133 | } |
| 134 | return $result; |
| 135 | } |
| 136 | /** |
| 137 | * Given a string containing a directory, this function will return the total |
| 138 | * number of bytes on the corresponding filesystem or disk partition. |
| 139 | * |
| 140 | * @param string $directory A directory of the filesystem or disk partition. |
| 141 | * @return float Returns the total number of bytes as a float. |
| 142 | * @throws FilesystemException |
| 143 | * |
| 144 | */ |
| 145 | function disk_total_space(string $directory): float |
| 146 | { |
| 147 | error_clear_last(); |
| 148 | $result = \disk_total_space($directory); |
| 149 | if ($result === \false) { |
| 150 | throw FilesystemException::createFromPhpError(); |
| 151 | } |
| 152 | return $result; |
| 153 | } |
| 154 | /** |
| 155 | * The file pointed to by handle is closed. |
| 156 | * |
| 157 | * @param resource $handle The file pointer must be valid, and must point to a file successfully |
| 158 | * opened by fopen or fsockopen. |
| 159 | * @throws FilesystemException |
| 160 | * |
| 161 | */ |
| 162 | function fclose($handle): void |
| 163 | { |
| 164 | error_clear_last(); |
| 165 | $result = \fclose($handle); |
| 166 | if ($result === \false) { |
| 167 | throw FilesystemException::createFromPhpError(); |
| 168 | } |
| 169 | } |
| 170 | /** |
| 171 | * This function forces a write of all buffered output to the resource |
| 172 | * pointed to by the file handle. |
| 173 | * |
| 174 | * @param resource $handle The file pointer must be valid, and must point to |
| 175 | * a file successfully opened by fopen or |
| 176 | * fsockopen (and not yet closed by |
| 177 | * fclose). |
| 178 | * @throws FilesystemException |
| 179 | * |
| 180 | */ |
| 181 | function fflush($handle): void |
| 182 | { |
| 183 | error_clear_last(); |
| 184 | $result = \fflush($handle); |
| 185 | if ($result === \false) { |
| 186 | throw FilesystemException::createFromPhpError(); |
| 187 | } |
| 188 | } |
| 189 | /** |
| 190 | * This function is similar to file, except that |
| 191 | * file_get_contents returns the file in a |
| 192 | * string, starting at the specified offset |
| 193 | * up to maxlen bytes. On failure, |
| 194 | * file_get_contents will return FALSE. |
| 195 | * |
| 196 | * file_get_contents is the preferred way to read the |
| 197 | * contents of a file into a string. It will use memory mapping techniques if |
| 198 | * supported by your OS to enhance performance. |
| 199 | * |
| 200 | * @param string $filename Name of the file to read. |
| 201 | * @param bool $use_include_path The FILE_USE_INCLUDE_PATH constant can be used |
| 202 | * to trigger include path |
| 203 | * search. |
| 204 | * This is not possible if strict typing |
| 205 | * is enabled, since FILE_USE_INCLUDE_PATH is an |
| 206 | * int. Use TRUE instead. |
| 207 | * @param resource|null $context A valid context resource created with |
| 208 | * stream_context_create. If you don't need to use a |
| 209 | * custom context, you can skip this parameter by NULL. |
| 210 | * @param int $offset The offset where the reading starts on the original stream. |
| 211 | * Negative offsets count from the end of the stream. |
| 212 | * |
| 213 | * Seeking (offset) is not supported with remote files. |
| 214 | * Attempting to seek on non-local files may work with small offsets, but this |
| 215 | * is unpredictable because it works on the buffered stream. |
| 216 | * @param int $maxlen Maximum length of data read. The default is to read until end |
| 217 | * of file is reached. Note that this parameter is applied to the |
| 218 | * stream processed by the filters. |
| 219 | * @return string The function returns the read data. |
| 220 | * @throws FilesystemException |
| 221 | * |
| 222 | */ |
| 223 | function file_get_contents(string $filename, bool $use_include_path = \false, $context = null, int $offset = 0, int $maxlen = null): string |
| 224 | { |
| 225 | error_clear_last(); |
| 226 | if ($maxlen !== null) { |
| 227 | $result = \file_get_contents($filename, $use_include_path, $context, $offset, $maxlen); |
| 228 | } elseif ($offset !== 0) { |
| 229 | $result = \file_get_contents($filename, $use_include_path, $context, $offset); |
| 230 | } elseif ($context !== null) { |
| 231 | $result = \file_get_contents($filename, $use_include_path, $context); |
| 232 | } else { |
| 233 | $result = \file_get_contents($filename, $use_include_path); |
| 234 | } |
| 235 | if ($result === \false) { |
| 236 | throw FilesystemException::createFromPhpError(); |
| 237 | } |
| 238 | return $result; |
| 239 | } |
| 240 | /** |
| 241 | * This function is identical to calling fopen, |
| 242 | * fwrite and fclose successively |
| 243 | * to write data to a file. |
| 244 | * |
| 245 | * If filename does not exist, the file is created. |
| 246 | * Otherwise, the existing file is overwritten, unless the |
| 247 | * FILE_APPEND flag is set. |
| 248 | * |
| 249 | * @param string $filename Path to the file where to write the data. |
| 250 | * @param mixed $data The data to write. Can be either a string, an |
| 251 | * array or a stream resource. |
| 252 | * |
| 253 | * If data is a stream resource, the |
| 254 | * remaining buffer of that stream will be copied to the specified file. |
| 255 | * This is similar with using stream_copy_to_stream. |
| 256 | * |
| 257 | * You can also specify the data parameter as a single |
| 258 | * dimension array. This is equivalent to |
| 259 | * file_put_contents($filename, implode('', $array)). |
| 260 | * @param int $flags The value of flags can be any combination of |
| 261 | * the following flags, joined with the binary OR (|) |
| 262 | * operator. |
| 263 | * |
| 264 | * |
| 265 | * Available flags |
| 266 | * |
| 267 | * |
| 268 | * |
| 269 | * Flag |
| 270 | * Description |
| 271 | * |
| 272 | * |
| 273 | * |
| 274 | * |
| 275 | * |
| 276 | * FILE_USE_INCLUDE_PATH |
| 277 | * |
| 278 | * |
| 279 | * Search for filename in the include directory. |
| 280 | * See include_path for more |
| 281 | * information. |
| 282 | * |
| 283 | * |
| 284 | * |
| 285 | * |
| 286 | * FILE_APPEND |
| 287 | * |
| 288 | * |
| 289 | * If file filename already exists, append |
| 290 | * the data to the file instead of overwriting it. |
| 291 | * |
| 292 | * |
| 293 | * |
| 294 | * |
| 295 | * LOCK_EX |
| 296 | * |
| 297 | * |
| 298 | * Acquire an exclusive lock on the file while proceeding to the |
| 299 | * writing. In other words, a flock call happens |
| 300 | * between the fopen call and the |
| 301 | * fwrite call. This is not identical to an |
| 302 | * fopen call with mode "x". |
| 303 | * |
| 304 | * |
| 305 | * |
| 306 | * |
| 307 | * |
| 308 | * @param resource $context A valid context resource created with |
| 309 | * stream_context_create. |
| 310 | * @return int This function returns the number of bytes that were written to the file. |
| 311 | * @throws FilesystemException |
| 312 | * |
| 313 | */ |
| 314 | function file_put_contents(string $filename, $data, int $flags = 0, $context = null): int |
| 315 | { |
| 316 | error_clear_last(); |
| 317 | if ($context !== null) { |
| 318 | $result = \file_put_contents($filename, $data, $flags, $context); |
| 319 | } else { |
| 320 | $result = \file_put_contents($filename, $data, $flags); |
| 321 | } |
| 322 | if ($result === \false) { |
| 323 | throw FilesystemException::createFromPhpError(); |
| 324 | } |
| 325 | return $result; |
| 326 | } |
| 327 | /** |
| 328 | * Reads an entire file into an array. |
| 329 | * |
| 330 | * @param string $filename Path to the file. |
| 331 | * @param int $flags The optional parameter flags can be one, or |
| 332 | * more, of the following constants: |
| 333 | * |
| 334 | * |
| 335 | * |
| 336 | * FILE_USE_INCLUDE_PATH |
| 337 | * |
| 338 | * |
| 339 | * |
| 340 | * Search for the file in the include_path. |
| 341 | * |
| 342 | * |
| 343 | * |
| 344 | * |
| 345 | * |
| 346 | * FILE_IGNORE_NEW_LINES |
| 347 | * |
| 348 | * |
| 349 | * |
| 350 | * Omit newline at the end of each array element |
| 351 | * |
| 352 | * |
| 353 | * |
| 354 | * |
| 355 | * |
| 356 | * FILE_SKIP_EMPTY_LINES |
| 357 | * |
| 358 | * |
| 359 | * |
| 360 | * Skip empty lines |
| 361 | * |
| 362 | * |
| 363 | * |
| 364 | * |
| 365 | * @param resource $context |
| 366 | * @return array Returns the file in an array. Each element of the array corresponds to a |
| 367 | * line in the file, with the newline still attached. Upon failure, |
| 368 | * file returns FALSE. |
| 369 | * @throws FilesystemException |
| 370 | * |
| 371 | */ |
| 372 | function file(string $filename, int $flags = 0, $context = null): array |
| 373 | { |
| 374 | error_clear_last(); |
| 375 | if ($context !== null) { |
| 376 | $result = \file($filename, $flags, $context); |
| 377 | } else { |
| 378 | $result = \file($filename, $flags); |
| 379 | } |
| 380 | if ($result === \false) { |
| 381 | throw FilesystemException::createFromPhpError(); |
| 382 | } |
| 383 | return $result; |
| 384 | } |
| 385 | /** |
| 386 | * |
| 387 | * |
| 388 | * @param string $filename Path to the file. |
| 389 | * @return int Returns the time the file was last accessed. |
| 390 | * The time is returned as a Unix timestamp. |
| 391 | * @throws FilesystemException |
| 392 | * |
| 393 | */ |
| 394 | function fileatime(string $filename): int |
| 395 | { |
| 396 | error_clear_last(); |
| 397 | $result = \fileatime($filename); |
| 398 | if ($result === \false) { |
| 399 | throw FilesystemException::createFromPhpError(); |
| 400 | } |
| 401 | return $result; |
| 402 | } |
| 403 | /** |
| 404 | * Gets the inode change time of a file. |
| 405 | * |
| 406 | * @param string $filename Path to the file. |
| 407 | * @return int Returns the time the file was last changed. |
| 408 | * The time is returned as a Unix timestamp. |
| 409 | * @throws FilesystemException |
| 410 | * |
| 411 | */ |
| 412 | function filectime(string $filename): int |
| 413 | { |
| 414 | error_clear_last(); |
| 415 | $result = \filectime($filename); |
| 416 | if ($result === \false) { |
| 417 | throw FilesystemException::createFromPhpError(); |
| 418 | } |
| 419 | return $result; |
| 420 | } |
| 421 | /** |
| 422 | * Gets the file inode. |
| 423 | * |
| 424 | * @param string $filename Path to the file. |
| 425 | * @return int Returns the inode number of the file. |
| 426 | * @throws FilesystemException |
| 427 | * |
| 428 | */ |
| 429 | function fileinode(string $filename): int |
| 430 | { |
| 431 | error_clear_last(); |
| 432 | $result = \fileinode($filename); |
| 433 | if ($result === \false) { |
| 434 | throw FilesystemException::createFromPhpError(); |
| 435 | } |
| 436 | return $result; |
| 437 | } |
| 438 | /** |
| 439 | * This function returns the time when the data blocks of a file were being |
| 440 | * written to, that is, the time when the content of the file was changed. |
| 441 | * |
| 442 | * @param string $filename Path to the file. |
| 443 | * @return int Returns the time the file was last modified. |
| 444 | * The time is returned as a Unix timestamp, which is |
| 445 | * suitable for the date function. |
| 446 | * @throws FilesystemException |
| 447 | * |
| 448 | */ |
| 449 | function filemtime(string $filename): int |
| 450 | { |
| 451 | error_clear_last(); |
| 452 | $result = \filemtime($filename); |
| 453 | if ($result === \false) { |
| 454 | throw FilesystemException::createFromPhpError(); |
| 455 | } |
| 456 | return $result; |
| 457 | } |
| 458 | /** |
| 459 | * Gets the file owner. |
| 460 | * |
| 461 | * @param string $filename Path to the file. |
| 462 | * @return int Returns the user ID of the owner of the file. |
| 463 | * The user ID is returned in numerical format, use |
| 464 | * posix_getpwuid to resolve it to a username. |
| 465 | * @throws FilesystemException |
| 466 | * |
| 467 | */ |
| 468 | function fileowner(string $filename): int |
| 469 | { |
| 470 | error_clear_last(); |
| 471 | $result = \fileowner($filename); |
| 472 | if ($result === \false) { |
| 473 | throw FilesystemException::createFromPhpError(); |
| 474 | } |
| 475 | return $result; |
| 476 | } |
| 477 | /** |
| 478 | * Gets the size for the given file. |
| 479 | * |
| 480 | * @param string $filename Path to the file. |
| 481 | * @return int Returns the size of the file in bytes, or FALSE (and generates an error |
| 482 | * of level E_WARNING) in case of an error. |
| 483 | * @throws FilesystemException |
| 484 | * |
| 485 | */ |
| 486 | function filesize(string $filename): int |
| 487 | { |
| 488 | error_clear_last(); |
| 489 | $result = \filesize($filename); |
| 490 | if ($result === \false) { |
| 491 | throw FilesystemException::createFromPhpError(); |
| 492 | } |
| 493 | return $result; |
| 494 | } |
| 495 | /** |
| 496 | * flock allows you to perform a simple reader/writer |
| 497 | * model which can be used on virtually every platform (including most Unix |
| 498 | * derivatives and even Windows). |
| 499 | * |
| 500 | * On versions of PHP before 5.3.2, the lock is released also by |
| 501 | * fclose (which is also called automatically when script |
| 502 | * finished). |
| 503 | * |
| 504 | * PHP supports a portable way of locking complete files in an advisory way |
| 505 | * (which means all accessing programs have to use the same way of locking |
| 506 | * or it will not work). By default, this function will block until the |
| 507 | * requested lock is acquired; this may be controlled with the LOCK_NB option documented below. |
| 508 | * |
| 509 | * @param resource $handle A file system pointer resource |
| 510 | * that is typically created using fopen. |
| 511 | * @param int $operation operation is one of the following: |
| 512 | * |
| 513 | * |
| 514 | * |
| 515 | * LOCK_SH to acquire a shared lock (reader). |
| 516 | * |
| 517 | * |
| 518 | * |
| 519 | * |
| 520 | * LOCK_EX to acquire an exclusive lock (writer). |
| 521 | * |
| 522 | * |
| 523 | * |
| 524 | * |
| 525 | * LOCK_UN to release a lock (shared or exclusive). |
| 526 | * |
| 527 | * |
| 528 | * |
| 529 | * |
| 530 | * It is also possible to add LOCK_NB as a bitmask to one |
| 531 | * of the above operations, if flock should not |
| 532 | * block during the locking attempt. |
| 533 | * @param int|null $wouldblock The optional third argument is set to 1 if the lock would block |
| 534 | * (EWOULDBLOCK errno condition). |
| 535 | * @throws FilesystemException |
| 536 | * |
| 537 | */ |
| 538 | function flock($handle, int $operation, ?int &$wouldblock = null): void |
| 539 | { |
| 540 | error_clear_last(); |
| 541 | $result = \flock($handle, $operation, $wouldblock); |
| 542 | if ($result === \false) { |
| 543 | throw FilesystemException::createFromPhpError(); |
| 544 | } |
| 545 | } |
| 546 | /** |
| 547 | * fopen binds a named resource, specified by |
| 548 | * filename, to a stream. |
| 549 | * |
| 550 | * @param string $filename If filename is of the form "scheme://...", it |
| 551 | * is assumed to be a URL and PHP will search for a protocol handler |
| 552 | * (also known as a wrapper) for that scheme. If no wrappers for that |
| 553 | * protocol are registered, PHP will emit a notice to help you track |
| 554 | * potential problems in your script and then continue as though |
| 555 | * filename specifies a regular file. |
| 556 | * |
| 557 | * If PHP has decided that filename specifies |
| 558 | * a local file, then it will try to open a stream on that file. |
| 559 | * The file must be accessible to PHP, so you need to ensure that |
| 560 | * the file access permissions allow this access. |
| 561 | * If you have enabled |
| 562 | * open_basedir further |
| 563 | * restrictions may apply. |
| 564 | * |
| 565 | * If PHP has decided that filename specifies |
| 566 | * a registered protocol, and that protocol is registered as a |
| 567 | * network URL, PHP will check to make sure that |
| 568 | * allow_url_fopen is |
| 569 | * enabled. If it is switched off, PHP will emit a warning and |
| 570 | * the fopen call will fail. |
| 571 | * |
| 572 | * The list of supported protocols can be found in . Some protocols (also referred to as |
| 573 | * wrappers) support context |
| 574 | * and/or php.ini options. Refer to the specific page for the |
| 575 | * protocol in use for a list of options which can be set. (e.g. |
| 576 | * php.ini value user_agent used by the |
| 577 | * http wrapper). |
| 578 | * |
| 579 | * On the Windows platform, be careful to escape any backslashes |
| 580 | * used in the path to the file, or use forward slashes. |
| 581 | * |
| 582 | * |
| 583 | * |
| 584 | * ]]> |
| 585 | * |
| 586 | * |
| 587 | * @param string $mode The mode parameter specifies the type of access |
| 588 | * you require to the stream. It may be any of the following: |
| 589 | * |
| 590 | * |
| 591 | * A list of possible modes for fopen |
| 592 | * using mode |
| 593 | * |
| 594 | * |
| 595 | * |
| 596 | * |
| 597 | * mode |
| 598 | * Description |
| 599 | * |
| 600 | * |
| 601 | * |
| 602 | * |
| 603 | * 'r' |
| 604 | * |
| 605 | * Open for reading only; place the file pointer at the |
| 606 | * beginning of the file. |
| 607 | * |
| 608 | * |
| 609 | * |
| 610 | * 'r+' |
| 611 | * |
| 612 | * Open for reading and writing; place the file pointer at |
| 613 | * the beginning of the file. |
| 614 | * |
| 615 | * |
| 616 | * |
| 617 | * 'w' |
| 618 | * |
| 619 | * Open for writing only; place the file pointer at the |
| 620 | * beginning of the file and truncate the file to zero length. |
| 621 | * If the file does not exist, attempt to create it. |
| 622 | * |
| 623 | * |
| 624 | * |
| 625 | * 'w+' |
| 626 | * |
| 627 | * Open for reading and writing; place the file pointer at |
| 628 | * the beginning of the file and truncate the file to zero |
| 629 | * length. If the file does not exist, attempt to create it. |
| 630 | * |
| 631 | * |
| 632 | * |
| 633 | * 'a' |
| 634 | * |
| 635 | * Open for writing only; place the file pointer at the end of |
| 636 | * the file. If the file does not exist, attempt to create it. |
| 637 | * In this mode, fseek has no effect, writes are always appended. |
| 638 | * |
| 639 | * |
| 640 | * |
| 641 | * 'a+' |
| 642 | * |
| 643 | * Open for reading and writing; place the file pointer at |
| 644 | * the end of the file. If the file does not exist, attempt to |
| 645 | * create it. In this mode, fseek only affects |
| 646 | * the reading position, writes are always appended. |
| 647 | * |
| 648 | * |
| 649 | * |
| 650 | * 'x' |
| 651 | * |
| 652 | * Create and open for writing only; place the file pointer at the |
| 653 | * beginning of the file. If the file already exists, the |
| 654 | * fopen call will fail by returning FALSE and |
| 655 | * generating an error of level E_WARNING. If |
| 656 | * the file does not exist, attempt to create it. This is equivalent |
| 657 | * to specifying O_EXCL|O_CREAT flags for the |
| 658 | * underlying open(2) system call. |
| 659 | * |
| 660 | * |
| 661 | * |
| 662 | * 'x+' |
| 663 | * |
| 664 | * Create and open for reading and writing; otherwise it has the |
| 665 | * same behavior as 'x'. |
| 666 | * |
| 667 | * |
| 668 | * |
| 669 | * 'c' |
| 670 | * |
| 671 | * Open the file for writing only. If the file does not exist, it is |
| 672 | * created. If it exists, it is neither truncated (as opposed to |
| 673 | * 'w'), nor the call to this function fails (as is |
| 674 | * the case with 'x'). The file pointer is |
| 675 | * positioned on the beginning of the file. This may be useful if it's |
| 676 | * desired to get an advisory lock (see flock) |
| 677 | * before attempting to modify the file, as using |
| 678 | * 'w' could truncate the file before the lock |
| 679 | * was obtained (if truncation is desired, |
| 680 | * ftruncate can be used after the lock is |
| 681 | * requested). |
| 682 | * |
| 683 | * |
| 684 | * |
| 685 | * 'c+' |
| 686 | * |
| 687 | * Open the file for reading and writing; otherwise it has the same |
| 688 | * behavior as 'c'. |
| 689 | * |
| 690 | * |
| 691 | * |
| 692 | * 'e' |
| 693 | * |
| 694 | * Set close-on-exec flag on the opened file descriptor. Only |
| 695 | * available in PHP compiled on POSIX.1-2008 conform systems. |
| 696 | * |
| 697 | * |
| 698 | * |
| 699 | * |
| 700 | * |
| 701 | * |
| 702 | * Different operating system families have different line-ending |
| 703 | * conventions. When you write a text file and want to insert a line |
| 704 | * break, you need to use the correct line-ending character(s) for your |
| 705 | * operating system. Unix based systems use \n as the |
| 706 | * line ending character, Windows based systems use \r\n |
| 707 | * as the line ending characters and Macintosh based systems (Mac OS Classic) used |
| 708 | * \r as the line ending character. |
| 709 | * |
| 710 | * If you use the wrong line ending characters when writing your files, you |
| 711 | * might find that other applications that open those files will "look |
| 712 | * funny". |
| 713 | * |
| 714 | * Windows offers a text-mode translation flag ('t') |
| 715 | * which will transparently translate \n to |
| 716 | * \r\n when working with the file. In contrast, you |
| 717 | * can also use 'b' to force binary mode, which will not |
| 718 | * translate your data. To use these flags, specify either |
| 719 | * 'b' or 't' as the last character |
| 720 | * of the mode parameter. |
| 721 | * |
| 722 | * The default translation mode is 'b'. |
| 723 | * You can use the 't' |
| 724 | * mode if you are working with plain-text files and you use |
| 725 | * \n to delimit your line endings in your script, but |
| 726 | * expect your files to be readable with applications such as old versions of notepad. You |
| 727 | * should use the 'b' in all other cases. |
| 728 | * |
| 729 | * If you specify the 't' flag when working with binary files, you |
| 730 | * may experience strange problems with your data, including broken image |
| 731 | * files and strange problems with \r\n characters. |
| 732 | * |
| 733 | * For portability, it is also strongly recommended that |
| 734 | * you re-write code that uses or relies upon the 't' |
| 735 | * mode so that it uses the correct line endings and |
| 736 | * 'b' mode instead. |
| 737 | * @param bool $use_include_path The optional third use_include_path parameter |
| 738 | * can be set to '1' or TRUE if you want to search for the file in the |
| 739 | * include_path, too. |
| 740 | * @param resource $context |
| 741 | * @return resource Returns a file pointer resource on success |
| 742 | * @throws FilesystemException |
| 743 | * |
| 744 | */ |
| 745 | function fopen(string $filename, string $mode, bool $use_include_path = \false, $context = null) |
| 746 | { |
| 747 | error_clear_last(); |
| 748 | if ($context !== null) { |
| 749 | $result = \fopen($filename, $mode, $use_include_path, $context); |
| 750 | } else { |
| 751 | $result = \fopen($filename, $mode, $use_include_path); |
| 752 | } |
| 753 | if ($result === \false) { |
| 754 | throw FilesystemException::createFromPhpError(); |
| 755 | } |
| 756 | return $result; |
| 757 | } |
| 758 | /** |
| 759 | * fputcsv formats a line (passed as a |
| 760 | * fields array) as CSV and writes it (terminated by a |
| 761 | * newline) to the specified file handle. |
| 762 | * |
| 763 | * @param resource $handle The file pointer must be valid, and must point to |
| 764 | * a file successfully opened by fopen or |
| 765 | * fsockopen (and not yet closed by |
| 766 | * fclose). |
| 767 | * @param array $fields An array of strings. |
| 768 | * @param string $delimiter The optional delimiter parameter sets the field |
| 769 | * delimiter (one character only). |
| 770 | * @param string $enclosure The optional enclosure parameter sets the field |
| 771 | * enclosure (one character only). |
| 772 | * @param string $escape_char The optional escape_char parameter sets the |
| 773 | * escape character (at most one character). |
| 774 | * An empty string ("") disables the proprietary escape mechanism. |
| 775 | * @return int Returns the length of the written string. |
| 776 | * @throws FilesystemException |
| 777 | * |
| 778 | */ |
| 779 | function fputcsv($handle, array $fields, string $delimiter = ",", string $enclosure = '"', string $escape_char = "\\"): int |
| 780 | { |
| 781 | error_clear_last(); |
| 782 | $result = \fputcsv($handle, $fields, $delimiter, $enclosure, $escape_char); |
| 783 | if ($result === \false) { |
| 784 | throw FilesystemException::createFromPhpError(); |
| 785 | } |
| 786 | return $result; |
| 787 | } |
| 788 | /** |
| 789 | * fread reads up to |
| 790 | * length bytes from the file pointer |
| 791 | * referenced by handle. Reading stops as soon as one |
| 792 | * of the following conditions is met: |
| 793 | * |
| 794 | * |
| 795 | * |
| 796 | * length bytes have been read |
| 797 | * |
| 798 | * |
| 799 | * |
| 800 | * |
| 801 | * EOF (end of file) is reached |
| 802 | * |
| 803 | * |
| 804 | * |
| 805 | * |
| 806 | * a packet becomes available or the |
| 807 | * socket timeout occurs (for network streams) |
| 808 | * |
| 809 | * |
| 810 | * |
| 811 | * |
| 812 | * if the stream is read buffered and it does not represent a plain file, at |
| 813 | * most one read of up to a number of bytes equal to the chunk size (usually |
| 814 | * 8192) is made; depending on the previously buffered data, the size of the |
| 815 | * returned data may be larger than the chunk size. |
| 816 | * |
| 817 | * |
| 818 | * |
| 819 | * |
| 820 | * @param resource $handle A file system pointer resource |
| 821 | * that is typically created using fopen. |
| 822 | * @param int $length Up to length number of bytes read. |
| 823 | * @return string Returns the read string. |
| 824 | * @throws FilesystemException |
| 825 | * |
| 826 | */ |
| 827 | function fread($handle, int $length): string |
| 828 | { |
| 829 | error_clear_last(); |
| 830 | $result = \fread($handle, $length); |
| 831 | if ($result === \false) { |
| 832 | throw FilesystemException::createFromPhpError(); |
| 833 | } |
| 834 | return $result; |
| 835 | } |
| 836 | /** |
| 837 | * Takes the filepointer, handle, and truncates the file to |
| 838 | * length, size. |
| 839 | * |
| 840 | * @param resource $handle The file pointer. |
| 841 | * |
| 842 | * The handle must be open for writing. |
| 843 | * @param int $size The size to truncate to. |
| 844 | * |
| 845 | * If size is larger than the file then the file |
| 846 | * is extended with null bytes. |
| 847 | * |
| 848 | * If size is smaller than the file then the file |
| 849 | * is truncated to that size. |
| 850 | * @throws FilesystemException |
| 851 | * |
| 852 | */ |
| 853 | function ftruncate($handle, int $size): void |
| 854 | { |
| 855 | error_clear_last(); |
| 856 | $result = \ftruncate($handle, $size); |
| 857 | if ($result === \false) { |
| 858 | throw FilesystemException::createFromPhpError(); |
| 859 | } |
| 860 | } |
| 861 | /** |
| 862 | * |
| 863 | * |
| 864 | * @param resource $handle A file system pointer resource |
| 865 | * that is typically created using fopen. |
| 866 | * @param string $string The string that is to be written. |
| 867 | * @param int $length If the length argument is given, writing will |
| 868 | * stop after length bytes have been written or |
| 869 | * the end of string is reached, whichever comes |
| 870 | * first. |
| 871 | * |
| 872 | * Note that if the length argument is given, |
| 873 | * then the magic_quotes_runtime |
| 874 | * configuration option will be ignored and no slashes will be |
| 875 | * stripped from string. |
| 876 | * @return int |
| 877 | * @throws FilesystemException |
| 878 | * |
| 879 | */ |
| 880 | function fwrite($handle, string $string, int $length = null): int |
| 881 | { |
| 882 | error_clear_last(); |
| 883 | if ($length !== null) { |
| 884 | $result = \fwrite($handle, $string, $length); |
| 885 | } else { |
| 886 | $result = \fwrite($handle, $string); |
| 887 | } |
| 888 | if ($result === \false) { |
| 889 | throw FilesystemException::createFromPhpError(); |
| 890 | } |
| 891 | return $result; |
| 892 | } |
| 893 | /** |
| 894 | * The glob function searches for all the pathnames |
| 895 | * matching pattern according to the rules used by |
| 896 | * the libc glob() function, which is similar to the rules used by common |
| 897 | * shells. |
| 898 | * |
| 899 | * @param string $pattern The pattern. No tilde expansion or parameter substitution is done. |
| 900 | * |
| 901 | * Special characters: |
| 902 | * |
| 903 | * |
| 904 | * |
| 905 | * * - Matches zero or more characters. |
| 906 | * |
| 907 | * |
| 908 | * |
| 909 | * |
| 910 | * ? - Matches exactly one character (any character). |
| 911 | * |
| 912 | * |
| 913 | * |
| 914 | * |
| 915 | * [...] - Matches one character from a group of |
| 916 | * characters. If the first character is !, |
| 917 | * matches any character not in the group. |
| 918 | * |
| 919 | * |
| 920 | * |
| 921 | * |
| 922 | * \ - Escapes the following character, |
| 923 | * except when the GLOB_NOESCAPE flag is used. |
| 924 | * |
| 925 | * |
| 926 | * |
| 927 | * @param int $flags Valid flags: |
| 928 | * |
| 929 | * |
| 930 | * |
| 931 | * GLOB_MARK - Adds a slash (a backslash on Windows) to each directory returned |
| 932 | * |
| 933 | * |
| 934 | * |
| 935 | * |
| 936 | * GLOB_NOSORT - Return files as they appear in the |
| 937 | * directory (no sorting). When this flag is not used, the pathnames are |
| 938 | * sorted alphabetically |
| 939 | * |
| 940 | * |
| 941 | * |
| 942 | * |
| 943 | * GLOB_NOCHECK - Return the search pattern if no |
| 944 | * files matching it were found |
| 945 | * |
| 946 | * |
| 947 | * |
| 948 | * |
| 949 | * GLOB_NOESCAPE - Backslashes do not quote |
| 950 | * metacharacters |
| 951 | * |
| 952 | * |
| 953 | * |
| 954 | * |
| 955 | * GLOB_BRACE - Expands {a,b,c} to match 'a', 'b', |
| 956 | * or 'c' |
| 957 | * |
| 958 | * |
| 959 | * |
| 960 | * |
| 961 | * GLOB_ONLYDIR - Return only directory entries |
| 962 | * which match the pattern |
| 963 | * |
| 964 | * |
| 965 | * |
| 966 | * |
| 967 | * GLOB_ERR - Stop on read errors (like unreadable |
| 968 | * directories), by default errors are ignored. |
| 969 | * |
| 970 | * |
| 971 | * |
| 972 | * @return array Returns an array containing the matched files/directories, an empty array |
| 973 | * if no file matched. |
| 974 | * @throws FilesystemException |
| 975 | * |
| 976 | */ |
| 977 | function glob(string $pattern, int $flags = 0): array |
| 978 | { |
| 979 | error_clear_last(); |
| 980 | $result = \glob($pattern, $flags); |
| 981 | if ($result === \false) { |
| 982 | throw FilesystemException::createFromPhpError(); |
| 983 | } |
| 984 | return $result; |
| 985 | } |
| 986 | /** |
| 987 | * Attempts to change the group of the symlink filename |
| 988 | * to group. |
| 989 | * |
| 990 | * Only the superuser may change the group of a symlink arbitrarily; other |
| 991 | * users may change the group of a symlink to any group of which that user is |
| 992 | * a member. |
| 993 | * |
| 994 | * @param string $filename Path to the symlink. |
| 995 | * @param string|int $group The group specified by name or number. |
| 996 | * @throws FilesystemException |
| 997 | * |
| 998 | */ |
| 999 | function lchgrp(string $filename, $group): void |
| 1000 | { |
| 1001 | error_clear_last(); |
| 1002 | $result = \lchgrp($filename, $group); |
| 1003 | if ($result === \false) { |
| 1004 | throw FilesystemException::createFromPhpError(); |
| 1005 | } |
| 1006 | } |
| 1007 | /** |
| 1008 | * Attempts to change the owner of the symlink filename |
| 1009 | * to user user. |
| 1010 | * |
| 1011 | * Only the superuser may change the owner of a symlink. |
| 1012 | * |
| 1013 | * @param string $filename Path to the file. |
| 1014 | * @param string|int $user User name or number. |
| 1015 | * @throws FilesystemException |
| 1016 | * |
| 1017 | */ |
| 1018 | function lchown(string $filename, $user): void |
| 1019 | { |
| 1020 | error_clear_last(); |
| 1021 | $result = \lchown($filename, $user); |
| 1022 | if ($result === \false) { |
| 1023 | throw FilesystemException::createFromPhpError(); |
| 1024 | } |
| 1025 | } |
| 1026 | /** |
| 1027 | * link creates a hard link. |
| 1028 | * |
| 1029 | * @param string $target Target of the link. |
| 1030 | * @param string $link The link name. |
| 1031 | * @throws FilesystemException |
| 1032 | * |
| 1033 | */ |
| 1034 | function link(string $target, string $link): void |
| 1035 | { |
| 1036 | error_clear_last(); |
| 1037 | $result = \link($target, $link); |
| 1038 | if ($result === \false) { |
| 1039 | throw FilesystemException::createFromPhpError(); |
| 1040 | } |
| 1041 | } |
| 1042 | /** |
| 1043 | * Attempts to create the directory specified by pathname. |
| 1044 | * |
| 1045 | * @param string $pathname The directory path. |
| 1046 | * @param int $mode The mode is 0777 by default, which means the widest possible |
| 1047 | * access. For more information on modes, read the details |
| 1048 | * on the chmod page. |
| 1049 | * |
| 1050 | * mode is ignored on Windows. |
| 1051 | * |
| 1052 | * Note that you probably want to specify the mode as an octal number, |
| 1053 | * which means it should have a leading zero. The mode is also modified |
| 1054 | * by the current umask, which you can change using |
| 1055 | * umask. |
| 1056 | * @param bool $recursive Allows the creation of nested directories specified in the |
| 1057 | * pathname. |
| 1058 | * @param resource $context |
| 1059 | * @throws FilesystemException |
| 1060 | * |
| 1061 | */ |
| 1062 | function mkdir(string $pathname, int $mode = 0777, bool $recursive = \false, $context = null): void |
| 1063 | { |
| 1064 | error_clear_last(); |
| 1065 | if ($context !== null) { |
| 1066 | $result = \mkdir($pathname, $mode, $recursive, $context); |
| 1067 | } else { |
| 1068 | $result = \mkdir($pathname, $mode, $recursive); |
| 1069 | } |
| 1070 | if ($result === \false) { |
| 1071 | throw FilesystemException::createFromPhpError(); |
| 1072 | } |
| 1073 | } |
| 1074 | /** |
| 1075 | * parse_ini_file loads in the |
| 1076 | * ini file specified in filename, |
| 1077 | * and returns the settings in it in an associative array. |
| 1078 | * |
| 1079 | * The structure of the ini file is the same as the php.ini's. |
| 1080 | * |
| 1081 | * @param string $filename The filename of the ini file being parsed. If a relative path is used, |
| 1082 | * it is evaluated relative to the current working directory, then the |
| 1083 | * include_path. |
| 1084 | * @param bool $process_sections By setting the process_sections |
| 1085 | * parameter to TRUE, you get a multidimensional array, with |
| 1086 | * the section names and settings included. The default |
| 1087 | * for process_sections is FALSE |
| 1088 | * @param int $scanner_mode Can either be INI_SCANNER_NORMAL (default) or |
| 1089 | * INI_SCANNER_RAW. If INI_SCANNER_RAW |
| 1090 | * is supplied, then option values will not be parsed. |
| 1091 | * |
| 1092 | * |
| 1093 | * As of PHP 5.6.1 can also be specified as INI_SCANNER_TYPED. |
| 1094 | * In this mode boolean, null and integer types are preserved when possible. |
| 1095 | * String values "true", "on" and "yes" |
| 1096 | * are converted to TRUE. "false", "off", "no" |
| 1097 | * and "none" are considered FALSE. "null" is converted to NULL |
| 1098 | * in typed mode. Also, all numeric strings are converted to integer type if it is possible. |
| 1099 | * @return array The settings are returned as an associative array on success. |
| 1100 | * @throws FilesystemException |
| 1101 | * |
| 1102 | */ |
| 1103 | function parse_ini_file(string $filename, bool $process_sections = \false, int $scanner_mode = \INI_SCANNER_NORMAL): array |
| 1104 | { |
| 1105 | error_clear_last(); |
| 1106 | $result = \parse_ini_file($filename, $process_sections, $scanner_mode); |
| 1107 | if ($result === \false) { |
| 1108 | throw FilesystemException::createFromPhpError(); |
| 1109 | } |
| 1110 | return $result; |
| 1111 | } |
| 1112 | /** |
| 1113 | * parse_ini_string returns the settings in string |
| 1114 | * ini in an associative array. |
| 1115 | * |
| 1116 | * The structure of the ini string is the same as the php.ini's. |
| 1117 | * |
| 1118 | * @param string $ini The contents of the ini file being parsed. |
| 1119 | * @param bool $process_sections By setting the process_sections |
| 1120 | * parameter to TRUE, you get a multidimensional array, with |
| 1121 | * the section names and settings included. The default |
| 1122 | * for process_sections is FALSE |
| 1123 | * @param int $scanner_mode Can either be INI_SCANNER_NORMAL (default) or |
| 1124 | * INI_SCANNER_RAW. If INI_SCANNER_RAW |
| 1125 | * is supplied, then option values will not be parsed. |
| 1126 | * |
| 1127 | * |
| 1128 | * As of PHP 5.6.1 can also be specified as INI_SCANNER_TYPED. |
| 1129 | * In this mode boolean, null and integer types are preserved when possible. |
| 1130 | * String values "true", "on" and "yes" |
| 1131 | * are converted to TRUE. "false", "off", "no" |
| 1132 | * and "none" are considered FALSE. "null" is converted to NULL |
| 1133 | * in typed mode. Also, all numeric strings are converted to integer type if it is possible. |
| 1134 | * @return array The settings are returned as an associative array on success. |
| 1135 | * @throws FilesystemException |
| 1136 | * |
| 1137 | */ |
| 1138 | function parse_ini_string(string $ini, bool $process_sections = \false, int $scanner_mode = \INI_SCANNER_NORMAL): array |
| 1139 | { |
| 1140 | error_clear_last(); |
| 1141 | $result = \parse_ini_string($ini, $process_sections, $scanner_mode); |
| 1142 | if ($result === \false) { |
| 1143 | throw FilesystemException::createFromPhpError(); |
| 1144 | } |
| 1145 | return $result; |
| 1146 | } |
| 1147 | /** |
| 1148 | * Reads a file and writes it to the output buffer. |
| 1149 | * |
| 1150 | * @param string $filename The filename being read. |
| 1151 | * @param bool $use_include_path You can use the optional second parameter and set it to TRUE, if |
| 1152 | * you want to search for the file in the include_path, too. |
| 1153 | * @param resource $context A context stream resource. |
| 1154 | * @return int Returns the number of bytes read from the file on success |
| 1155 | * @throws FilesystemException |
| 1156 | * |
| 1157 | */ |
| 1158 | function readfile(string $filename, bool $use_include_path = \false, $context = null): int |
| 1159 | { |
| 1160 | error_clear_last(); |
| 1161 | if ($context !== null) { |
| 1162 | $result = \readfile($filename, $use_include_path, $context); |
| 1163 | } else { |
| 1164 | $result = \readfile($filename, $use_include_path); |
| 1165 | } |
| 1166 | if ($result === \false) { |
| 1167 | throw FilesystemException::createFromPhpError(); |
| 1168 | } |
| 1169 | return $result; |
| 1170 | } |
| 1171 | /** |
| 1172 | * readlink does the same as the readlink C function. |
| 1173 | * |
| 1174 | * @param string $path The symbolic link path. |
| 1175 | * @return string Returns the contents of the symbolic link path. |
| 1176 | * @throws FilesystemException |
| 1177 | * |
| 1178 | */ |
| 1179 | function readlink(string $path): string |
| 1180 | { |
| 1181 | error_clear_last(); |
| 1182 | $result = \readlink($path); |
| 1183 | if ($result === \false) { |
| 1184 | throw FilesystemException::createFromPhpError(); |
| 1185 | } |
| 1186 | return $result; |
| 1187 | } |
| 1188 | /** |
| 1189 | * realpath expands all symbolic links and |
| 1190 | * resolves references to /./, /../ and extra / characters in |
| 1191 | * the input path and returns the canonicalized |
| 1192 | * absolute pathname. |
| 1193 | * |
| 1194 | * @param string $path The path being checked. |
| 1195 | * |
| 1196 | * |
| 1197 | * Whilst a path must be supplied, the value can be an empty string. |
| 1198 | * In this case, the value is interpreted as the current directory. |
| 1199 | * |
| 1200 | * |
| 1201 | * |
| 1202 | * Whilst a path must be supplied, the value can be an empty string. |
| 1203 | * In this case, the value is interpreted as the current directory. |
| 1204 | * @return string Returns the canonicalized absolute pathname on success. The resulting path |
| 1205 | * will have no symbolic link, /./ or /../ components. Trailing delimiters, |
| 1206 | * such as \ and /, are also removed. |
| 1207 | * |
| 1208 | * realpath returns FALSE on failure, e.g. if |
| 1209 | * the file does not exist. |
| 1210 | * @throws FilesystemException |
| 1211 | * |
| 1212 | */ |
| 1213 | function realpath(string $path): string |
| 1214 | { |
| 1215 | error_clear_last(); |
| 1216 | $result = \realpath($path); |
| 1217 | if ($result === \false) { |
| 1218 | throw FilesystemException::createFromPhpError(); |
| 1219 | } |
| 1220 | return $result; |
| 1221 | } |
| 1222 | /** |
| 1223 | * Attempts to rename oldname to |
| 1224 | * newname, moving it between directories if necessary. |
| 1225 | * If renaming a file and newname exists, |
| 1226 | * it will be overwritten. If renaming a directory and |
| 1227 | * newname exists, |
| 1228 | * this function will emit a warning. |
| 1229 | * |
| 1230 | * @param string $oldname The old name. |
| 1231 | * |
| 1232 | * The wrapper used in oldname |
| 1233 | * must match the wrapper used in |
| 1234 | * newname. |
| 1235 | * @param string $newname The new name. |
| 1236 | * @param resource $context |
| 1237 | * @throws FilesystemException |
| 1238 | * |
| 1239 | */ |
| 1240 | function rename(string $oldname, string $newname, $context = null): void |
| 1241 | { |
| 1242 | error_clear_last(); |
| 1243 | if ($context !== null) { |
| 1244 | $result = \rename($oldname, $newname, $context); |
| 1245 | } else { |
| 1246 | $result = \rename($oldname, $newname); |
| 1247 | } |
| 1248 | if ($result === \false) { |
| 1249 | throw FilesystemException::createFromPhpError(); |
| 1250 | } |
| 1251 | } |
| 1252 | /** |
| 1253 | * Sets the file position indicator for handle |
| 1254 | * to the beginning of the file stream. |
| 1255 | * |
| 1256 | * @param resource $handle The file pointer must be valid, and must point to a file |
| 1257 | * successfully opened by fopen. |
| 1258 | * @throws FilesystemException |
| 1259 | * |
| 1260 | */ |
| 1261 | function rewind($handle): void |
| 1262 | { |
| 1263 | error_clear_last(); |
| 1264 | $result = \rewind($handle); |
| 1265 | if ($result === \false) { |
| 1266 | throw FilesystemException::createFromPhpError(); |
| 1267 | } |
| 1268 | } |
| 1269 | /** |
| 1270 | * Attempts to remove the directory named by dirname. |
| 1271 | * The directory must be empty, and the relevant permissions must permit this. |
| 1272 | * A E_WARNING level error will be generated on failure. |
| 1273 | * |
| 1274 | * @param string $dirname Path to the directory. |
| 1275 | * @param resource $context |
| 1276 | * @throws FilesystemException |
| 1277 | * |
| 1278 | */ |
| 1279 | function rmdir(string $dirname, $context = null): void |
| 1280 | { |
| 1281 | error_clear_last(); |
| 1282 | if ($context !== null) { |
| 1283 | $result = \rmdir($dirname, $context); |
| 1284 | } else { |
| 1285 | $result = \rmdir($dirname); |
| 1286 | } |
| 1287 | if ($result === \false) { |
| 1288 | throw FilesystemException::createFromPhpError(); |
| 1289 | } |
| 1290 | } |
| 1291 | /** |
| 1292 | * symlink creates a symbolic link to the existing |
| 1293 | * target with the specified name |
| 1294 | * link. |
| 1295 | * |
| 1296 | * @param string $target Target of the link. |
| 1297 | * @param string $link The link name. |
| 1298 | * @throws FilesystemException |
| 1299 | * |
| 1300 | */ |
| 1301 | function symlink(string $target, string $link): void |
| 1302 | { |
| 1303 | error_clear_last(); |
| 1304 | $result = \symlink($target, $link); |
| 1305 | if ($result === \false) { |
| 1306 | throw FilesystemException::createFromPhpError(); |
| 1307 | } |
| 1308 | } |
| 1309 | /** |
| 1310 | * Creates a file with a unique filename, with access permission set to 0600, in the specified directory. |
| 1311 | * If the directory does not exist or is not writable, tempnam may |
| 1312 | * generate a file in the system's temporary directory, and return |
| 1313 | * the full path to that file, including its name. |
| 1314 | * |
| 1315 | * @param string $dir The directory where the temporary filename will be created. |
| 1316 | * @param string $prefix The prefix of the generated temporary filename. |
| 1317 | * @return string Returns the new temporary filename (with path). |
| 1318 | * @throws FilesystemException |
| 1319 | * |
| 1320 | */ |
| 1321 | function tempnam(string $dir, string $prefix): string |
| 1322 | { |
| 1323 | error_clear_last(); |
| 1324 | $result = \tempnam($dir, $prefix); |
| 1325 | if ($result === \false) { |
| 1326 | throw FilesystemException::createFromPhpError(); |
| 1327 | } |
| 1328 | return $result; |
| 1329 | } |
| 1330 | /** |
| 1331 | * Creates a temporary file with a unique name in read-write (w+) mode and |
| 1332 | * returns a file handle. |
| 1333 | * |
| 1334 | * The file is automatically removed when closed (for example, by calling |
| 1335 | * fclose, or when there are no remaining references to |
| 1336 | * the file handle returned by tmpfile), or when the |
| 1337 | * script ends. |
| 1338 | * |
| 1339 | * @return resource Returns a file handle, similar to the one returned by |
| 1340 | * fopen, for the new file. |
| 1341 | * @throws FilesystemException |
| 1342 | * |
| 1343 | */ |
| 1344 | function tmpfile() |
| 1345 | { |
| 1346 | error_clear_last(); |
| 1347 | $result = \tmpfile(); |
| 1348 | if ($result === \false) { |
| 1349 | throw FilesystemException::createFromPhpError(); |
| 1350 | } |
| 1351 | return $result; |
| 1352 | } |
| 1353 | /** |
| 1354 | * Attempts to set the access and modification times of the file named in the |
| 1355 | * filename parameter to the value given in |
| 1356 | * time. |
| 1357 | * Note that the access time is always modified, regardless of the number |
| 1358 | * of parameters. |
| 1359 | * |
| 1360 | * If the file does not exist, it will be created. |
| 1361 | * |
| 1362 | * @param string $filename The name of the file being touched. |
| 1363 | * @param int $time The touch time. If time is not supplied, |
| 1364 | * the current system time is used. |
| 1365 | * @param int $atime If present, the access time of the given filename is set to |
| 1366 | * the value of atime. Otherwise, it is set to |
| 1367 | * the value passed to the time parameter. |
| 1368 | * If neither are present, the current system time is used. |
| 1369 | * @throws FilesystemException |
| 1370 | * |
| 1371 | */ |
| 1372 | function touch(string $filename, int $time = null, int $atime = null): void |
| 1373 | { |
| 1374 | error_clear_last(); |
| 1375 | if ($atime !== null) { |
| 1376 | $result = \touch($filename, $time, $atime); |
| 1377 | } elseif ($time !== null) { |
| 1378 | $result = \touch($filename, $time); |
| 1379 | } else { |
| 1380 | $result = \touch($filename); |
| 1381 | } |
| 1382 | if ($result === \false) { |
| 1383 | throw FilesystemException::createFromPhpError(); |
| 1384 | } |
| 1385 | } |
| 1386 | /** |
| 1387 | * Deletes filename. Similar to the Unix C unlink() |
| 1388 | * function. An E_WARNING level error will be generated on |
| 1389 | * failure. |
| 1390 | * |
| 1391 | * @param string $filename Path to the file. |
| 1392 | * @param resource $context |
| 1393 | * @throws FilesystemException |
| 1394 | * |
| 1395 | */ |
| 1396 | function unlink(string $filename, $context = null): void |
| 1397 | { |
| 1398 | error_clear_last(); |
| 1399 | if ($context !== null) { |
| 1400 | $result = \unlink($filename, $context); |
| 1401 | } else { |
| 1402 | $result = \unlink($filename); |
| 1403 | } |
| 1404 | if ($result === \false) { |
| 1405 | throw FilesystemException::createFromPhpError(); |
| 1406 | } |
| 1407 | } |
| 1408 |