1
0
mirror of https://github.com/danog/file.git synced 2024-12-12 09:19:48 +01:00
file/README.md

171 lines
6.0 KiB
Markdown
Raw Normal View History

2022-09-16 20:46:09 +02:00
# amphp/file
2015-07-11 03:59:39 +02:00
2022-09-16 20:46:09 +02:00
AMPHP is a collection of event-driven libraries for PHP designed with fibers and concurrency in mind.
2023-08-18 20:19:11 +02:00
This package provides an abstraction layer and non-blocking file access solution that keeps your application responsive.
2022-09-16 20:46:09 +02:00
[![Latest Release](https://img.shields.io/github/release/amphp/file.svg?style=flat-square)](https://github.com/amphp/file/releases)
2023-08-18 20:19:11 +02:00
[![MIT License](https://img.shields.io/badge/license-MIT-blue.svg?style=flat-square)](https://github.com/amphp/file/blob/3.x/LICENSE)
2017-06-21 10:26:39 +02:00
## Installation
2015-07-11 03:59:39 +02:00
2017-06-21 10:26:39 +02:00
This package can be installed as a [Composer](https://getcomposer.org/) dependency.
2015-07-11 03:59:39 +02:00
2017-06-21 10:26:39 +02:00
```bash
composer require amphp/file
```
2015-07-11 03:59:39 +02:00
2022-09-16 20:46:09 +02:00
`amphp/file` works out of the box without any PHP extensions.
2023-08-18 20:19:11 +02:00
It uses multiple processes by default, but also comes with a blocking driver that uses PHP's blocking functions in the current process.
2022-09-16 20:46:09 +02:00
2023-08-18 20:19:11 +02:00
Extensions allow using threading in the background instead of using multiple processes and will automatically be used if they're available:
2022-09-16 20:46:09 +02:00
- [`ext-eio`](https://pecl.php.net/package/eio)
- [`ext-uv`](https://github.com/amphp/ext-uv)
- [`ext-parallel`](https://github.com/krakjoe/parallel)
## Usage
2023-08-18 20:19:11 +02:00
### Reading Files
2022-09-16 20:46:09 +02:00
Read the specified file's contents:
```php
$contents = Amp\File\read('/path/to/file.txt');
```
2023-08-18 20:19:11 +02:00
### Writing Files
2022-09-16 20:46:09 +02:00
Write the contents string to the specified path:
```php
Amp\File\write('/path/to/file.txt', 'contents');
```
2023-08-18 20:19:11 +02:00
### File Handles
2022-09-16 20:46:09 +02:00
2023-08-18 20:19:11 +02:00
Instead of reading or writing an entire file, the library also allows opening a `File` handle, e.g. to stream data:
2022-09-16 20:46:09 +02:00
```php
$file = Amp\File\openFile('/path/to/file.txt', 'r');
Amp\ByteStream\pipe($file, getStdout());
```
2023-08-18 20:19:11 +02:00
- `File::read()`: See also `File::isReadable`.
- `File::write()`: See also `File::isWritable`.
- `File::end()`: See `WritableStream::end()`.
- `File::close()`: Close the file handle.
- `File::isClosed()` can be used to check if the file handle has already been closed.
- `File::onClose()` can be used ot register a callback that's called on file handle closure.
- `File::seek()`: Set the internal pointer position and returns the new offset position.
- `SEEK_SET`: Set position equal to offset bytes.
- `SEEK_CUR`: Set position to current location plus offset.
- `SEEK_END`: Set position to end-of-file plus offset.
- `File::isSeekable`: Not documented, yet.
- `File::tell()`: Return the current internal offset position of the file handle.
- `File::eof()`: Test for being at the end of the stream (a.k.a. "end-of-file").
- `File::getPath()`: Retrieve the path used when opening the file handle.
- `File::getMode()`: Retrieve the mode used when opening the file handle.
- `File::truncate()`: Truncates the file to the given length. If `$size` is larger than the current file size, the file is extended with NUL bytes.
2022-09-16 20:46:09 +02:00
2023-08-18 20:19:11 +02:00
### Metadata
2022-09-16 20:46:09 +02:00
2023-08-18 20:19:11 +02:00
File metadata refers to the descriptive information about a file, distinct from its actual content. This includes details like the file's size, permissions, creation and modification timestamps, ownership, and file type. Metadata provides essential context about the file's attributes, enabling users and applications to understand its characteristics without delving into the content. Unlike the file's actual data, which holds the meaningful information or instructions, metadata focuses on administrative and structural attributes that aid in file organization, access, and management.
2022-09-16 20:46:09 +02:00
2023-08-18 20:19:11 +02:00
#### Existence
2022-09-16 20:46:09 +02:00
2023-08-18 20:19:11 +02:00
- `exists()`: Checks whether the specified path exists.
2022-09-16 20:46:09 +02:00
2023-08-18 20:19:11 +02:00
#### File Size
2022-09-16 20:46:09 +02:00
2023-08-18 20:19:11 +02:00
- `getSize()`: Returns the file size in bytes.
2022-09-16 20:46:09 +02:00
2023-08-18 20:19:11 +02:00
#### File Type
2022-09-16 20:46:09 +02:00
2023-08-18 20:19:11 +02:00
- `isDirectory()`: Checks whether the given path exists and is a directory.
- `isFile()`: Checks whether the given path exists and is a regular file.
- `isSymlink()`: Checks whether the given path exists and is a symlink.
2022-09-16 20:46:09 +02:00
2023-08-18 20:19:11 +02:00
#### File Timestamps
2022-09-16 20:46:09 +02:00
2023-08-18 20:19:11 +02:00
- `getModificationTime()`: Returns the file's modification time as Unix timestamp in seconds.
- `getAccessTime()`: Returns the file's access time as Unix timestamp in seconds.
- `getCreationTime()`: Returns the file's creation time as Unix timestamp in seconds.
2022-09-16 20:46:09 +02:00
2023-08-18 20:19:11 +02:00
#### Low Level Access
2022-09-16 20:46:09 +02:00
2023-08-18 20:19:11 +02:00
> **Note**
> We highly recommend to work with the higher-level APIs like `exists()` if possible instead of directly working with the `getStatus()` / `getLinkStatus()` API.
2022-09-16 20:46:09 +02:00
2023-08-18 20:19:11 +02:00
File metadata can be obtained using the `getStatus()` function, or `getLinkStatus()` in case of symlinks if you want to access the link metadata instead of the target file. Either function will return `null` if the path doesn't exist.
2022-09-16 20:46:09 +02:00
2023-08-18 20:19:11 +02:00
```php
<?php
2022-09-16 20:46:09 +02:00
2023-08-18 20:19:11 +02:00
use Amp\File;
2022-09-16 20:46:09 +02:00
2023-08-18 20:19:11 +02:00
require __DIR__ . '/../vendor/autoload.php';
2022-09-16 20:46:09 +02:00
2023-08-18 20:19:11 +02:00
var_dump(File\getStatus(__FILE__));
```
2022-09-16 20:46:09 +02:00
2023-08-18 20:19:11 +02:00
```plain
array(13) {
["dev"]=>
int(16777232)
["ino"]=>
int(15186622)
["mode"]=>
int(33188)
["nlink"]=>
int(1)
["uid"]=>
int(501)
["gid"]=>
int(20)
["rdev"]=>
int(0)
["size"]=>
int(104)
["blksize"]=>
int(4096)
["blocks"]=>
int(8)
["atime"]=>
int(1692381227)
["mtime"]=>
int(1692381226)
["ctime"]=>
int(1692381226)
}
```
2022-09-16 20:46:09 +02:00
2023-08-18 20:19:11 +02:00
### File Organization
2022-09-16 20:46:09 +02:00
2023-08-18 20:19:11 +02:00
- `move()`: Move / rename a file or directory.
- `deleteFile()`: Deletes a file.
- `createDirectory()`: Creates a directory.
- `createDirectoriesRecursively()`: Creates a directory and its parents.
- `deleteDirectory()`: Deletes a directory.
- `listFiles()`: List all files and subdirectories in a directory.
- `changePermissions()`: Change the permissions of a file or directory.
- `changeOwner()`: Change the ownership of a file or directory.
- `touch()`: Update the access and modification time of the specified path. If the file does not exist it will be created automatically.
2015-07-11 03:59:39 +02:00
2023-08-18 20:19:11 +02:00
### Links
2015-07-11 03:59:39 +02:00
2023-08-18 20:19:11 +02:00
- `createHardlink()`: Creates a new hardlink.
- `createSymlink()`: Creates a new symlink.
- `resolveSymlink()`: Resolves a symlink to its target path.
2015-07-11 03:59:39 +02:00
2017-06-21 10:26:39 +02:00
## Versioning
2015-07-11 03:59:39 +02:00
2021-06-21 21:08:24 +02:00
`amphp/file` follows the [semver](https://semver.org/) semantic versioning specification like all other `amphp` packages.
2017-06-21 10:26:39 +02:00
## Security
If you discover any security related issues, please email [`me@kelunik.com`](mailto:me@kelunik.com) instead of using the issue tracker.
2017-06-21 10:26:39 +02:00
## License
The MIT License (MIT). Please see [`LICENSE`](./LICENSE) for more information.