定制 amphp/file 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

amphp/file

Composer 安装命令:

composer require amphp/file

包简介

Non-blocking access to the filesystem based on Amp and Revolt.

README 文档

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.

Latest Release MIT License

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 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.

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 the getStatus() / 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.

amphp/file 适用场景与选型建议

amphp/file 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 3.39M 次下载、GitHub Stars 达 110, 最近一次更新时间为 2015 年 08 月 05 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「file」 「filesystem」 「static」 「io」 「async」 「non-blocking」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 amphp/file 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 amphp/file 我们能提供哪些服务?
定制开发 / 二次开发

基于 amphp/file 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 3.39M
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 111
  • 点击次数: 34
  • 依赖项目数: 98
  • 推荐数: 3

GitHub 信息

  • Stars: 110
  • Watchers: 11
  • Forks: 27
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2015-08-05