sandromiguel / php-streams
遵循 PSR-7 标准的流处理 PHP 库。
v1.2.0
2024-03-14 11:45 UTC
Requires
- php: >=8.1
- psr/http-message: ^2.0
Requires (Dev)
- nunomaduro/phpinsights: ^2.11
- phan/phan: ^5.4
- phpmetrics/phpmetrics: ^2.8
- phpspec/phpspec: ^7.5
- phpstan/phpstan: ^1.10
- phpunit/phpunit: ^9
- psalm/plugin-phpunit: ^0.18.4
- roave/security-advisories: dev-master
- vimeo/psalm: ^5.22
This package is auto-updated.
Last update: 2024-09-14 14:14:21 UTC
README
PhpStreams 是一个遵循 PSR-7 标准的流处理 PHP 库。
特性
- 流操作:轻松从流中读取和写入。
- PSR-7 兼容性:符合 PSR-7 标准以实现互操作性。
- 灵活:提供了一套完整的流操作功能。
安装
您可以通过 Composer 安装 PhpStreams
composer require sandromiguel/php-streams
使用方法
在 replit.com 上尝试
在 replit.com 平台上尝试交互式示例
内存中的文本流
require 'vendor/autoload.php'; use PhpStreams\Stream; // Create a text stream in memory $stream = new Stream(fopen('php://temp', 'r+')); // Write data to the stream $stream->write("Hello, world!\n"); // Move the pointer to the beginning of the stream $stream->rewind(); // Read data from the stream $data = $stream->getContents(); echo $data;
输出示例
Hello, world!
从文件中读取
require 'vendor/autoload.php'; use PhpStreams\Stream; // Open a file for reading $fileHandle = fopen('example.txt', 'r'); // Create a stream from the file handle $fileStream = new Stream($fileHandle); // Check if the stream is readable $fileContents = $fileStream->isReadable() ? $fileStream->getContents() : null; if ($fileContents) { echo $fileContents; } else { echo "The file is not readable."; } // Close the file handle fclose($fileHandle);
输出示例(如果 example.txt 包含 "Hello, my name is example.txt")
Hello, my name is example.txt
向文件中写入
require 'vendor/autoload.php'; use PhpStreams\Stream; // Open a file for writing $fileHandle = fopen('write.txt', 'w'); // Create a stream from the file handle $fileStream = new Stream($fileHandle); // Check if the stream is writable $bytesWritten = $fileStream->isWritable() ? $fileStream->write('New text') : null; if ($bytesWritten) { echo "Bytes written: $bytesWritten"; } else { echo "The file is not writable."; } // Close the file handle fclose($fileHandle);
输出示例(如果写入成功)
Bytes written: 8
从文件中读取指定字节数
require 'vendor/autoload.php'; use PhpStreams\Stream; // Open a file for reading $fileHandle = fopen('example.txt', 'r'); // Create a stream from the file handle $fileStream = new Stream($fileHandle); // Check if the stream is readable if ($fileStream->isReadable()) { // Define the exact number of bytes to read $numBytesToRead = 6; // Read 10 bytes from the file $data = $fileStream->read($numBytesToRead); // Output the read data echo "Read $numBytesToRead bytes of data: $data\n"; // Read the remaining content of the file $remainingData = $fileStream->getContents(); // Output the remaining data echo "Remaining data: $remainingData"; } else { echo "The file is not readable."; } // Close the file handle fclose($fileHandle);
输出示例
Read 6 bytes of data: Hello,
Remaining data: my name is example.txt
获取流元数据
require 'vendor/autoload.php'; use PhpStreams\Stream; // Open a file for reading $fileHandle = fopen('example.txt', 'r'); // Create a stream from the file handle $fileStream = new Stream($fileHandle); // Get metadata of the stream $metadata = $fileStream->getMetadata(); // Output the metadata echo "Stream metadata:\n"; print_r($metadata); // Close the file handle fclose($fileHandle);
输出示例
Stream metadata:
Array
(
[timed_out] =>
[blocked] => 1
[eof] =>
[wrapper_type] => plainfile
[stream_type] => STDIO
[mode] => r
[unread_bytes] => 0
[seekable] => 1
[uri] => example.txt
)
贡献
想要贡献?所有贡献都受欢迎。阅读 贡献指南。
问题
如果您有任何问题,请在 @sandro_m_m 上向我发推文或 打开一个问题。
许可
本项目受 MIT 许可证许可 - 请参阅 LICENSE 文件以获取详细信息
**~ 分享就是关爱 ~**