amphp / file
Non-blocking access to the filesystem based on Amp and Revolt.
Fund package maintenance!
amphp
Installs: 2 630 261
Dependents: 77
Suggesters: 3
Security: 0
Stars: 99
Watchers: 13
Forks: 25
Open Issues: 9
Requires
- php: >=8.1
- amphp/amp: ^3
- amphp/byte-stream: ^2
- amphp/cache: ^2
- amphp/parallel: ^2.1
- amphp/sync: ^2
- revolt/event-loop: ^1
Requires (Dev)
- amphp/php-cs-fixer-config: ^2
- amphp/phpunit-util: ^3
- phpunit/phpunit: ^9
- psalm/phar: 5.22.2
Suggests
- ext-eio: ^2 || ^3
- ext-uv: ^0.3 || ^0.2
- 3.x-dev
- v3.1.1
- v3.1.0
- v3.0.2
- v3.0.1
- v3.0.0
- v3.0.0-beta.6
- v3.0.0-beta.5
- v3.0.0-beta.4
- v3.0.0-beta.3
- v3.0.0-beta.2
- v3.0.0-beta.1
- 2.x-dev
- v2.0.4
- v2.0.3
- v2.0.2
- v2.0.1
- v2.0.0
- v1.x-dev
- v1.0.2
- v1.0.1
- v1.0.0
- v0.3.x-dev
- v0.3.5
- v0.3.4
- v0.3.3
- v0.3.2
- v0.3.1
- v0.3.0
- v0.2.4
- v0.2.3
- v0.2.2
- v0.2.1
- v0.2.0
- v0.1.3
- v0.1.2
- v0.1.1
- v0.1.0
- dev-no-global-pool-reuse
This package is auto-updated.
Last update: 2024-11-09 14:58:17 UTC
README
AMPHP is a collection of event-driven libraries for PHP designed with fibers and concurrency in mind. This package provides an abstraction layer and non-blocking file access solution that keeps your application responsive.
Installation
This package can be installed as a Composer dependency.
composer require amphp/file
Requirements
- PHP 8.1+
amphp/file
works out of the box without any PHP extensions.
It uses multiple processes by default, but also comes with a blocking driver that uses PHP's blocking functions in the current process.
Extensions allow using threading in the background instead of using multiple processes and will automatically be used if they're available:
Usage
Reading Files
Read the specified file's contents:
$contents = Amp\File\read('/path/to/file.txt');
Writing Files
Write the contents string to the specified path:
Amp\File\write('/path/to/file.txt', 'contents');
File Handles
Instead of reading or writing an entire file, the library also allows opening a File
handle, e.g. to stream data:
$file = Amp\File\openFile('/path/to/file.txt', 'r'); Amp\ByteStream\pipe($file, getStdout());
File::read()
: See alsoFile::isReadable
.File::write()
: See alsoFile::isWritable
.File::end()
: SeeWritableStream::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.
Metadata
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.
Existence
exists()
: Checks whether the specified path exists.
File Size
getSize()
: Returns the file size in bytes.
File Type
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.
File Timestamps
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.
Low Level Access
Note We highly recommend to work with the higher-level APIs like
exists()
if possible instead of directly working with thegetStatus()
/getLinkStatus()
API.
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.
<?php use Amp\File; require __DIR__ . '/../vendor/autoload.php'; var_dump(File\getStatus(__FILE__));
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)
}
File Organization
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.
Links
createHardlink()
: Creates a new hardlink.createSymlink()
: Creates a new symlink.resolveSymlink()
: Resolves a symlink to its target path.
Versioning
amphp/file
follows the semver semantic versioning specification like all other amphp
packages.
Security
If you discover any security related issues, please use the private security issue reporter instead of using the public issue tracker.
License
The MIT License (MIT). Please see LICENSE
for more information.