fostam/file 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

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

fostam/file

Composer 安装命令:

composer require fostam/file

包简介

Convenience wrapper for PHP functions like `fopen()`, `fgets()` that offers object oriented usage and handles errors with exceptions instead of return results.

README 文档

README

File is a simple convenience wrapper for PHP functions like fopen(), fgets(). It offers object oriented usage of file functions, and handles errors with exceptions instead of return results and PHP warnings/errors.

Install

The easiest way to install File is by using composer:

$> composer require fostam/file

Usage

Example: print the contents of a file, line by line

<?php

use Fostam\File\File;

$reader = new File($filename, File::MODE_READ);

while ($line = $reader->readLine()) {
    print $line;
}

$reader->close();

Example: write into a file

$writer = new File($filename, File::MODE_WRITE_CREATE_OR_TRUNCATE);
$writer->write('test');
$writer->close();

Example: resumt reading a file from a known position

$reader = new File($filename, File::MODE_READ);

// (read $pos from previous run)

while ($line = $reader->readLine(null, $pos)) {
    // (process $line)
    // (save $pos to resume if interrupted)
}

$reader->close();

Errors

All errors are caught and passed on by throwing an Exception. All thrown Exceptions derive from the Fostam\File\Exception\FileException class.

Reference

Methods

Method Return value Description Equivalent
__construct(string $filename, string $mode) void Constructor -
open() void Open the file fopen()
close(bool $silent = false) void Close the file fclose()
setPos(int $pos) void Set the file pointer to position $pos fseek()
getPos() int Get the current file position ftell()
readLine(?int $maxBytes = null, int $pos = null) ?string Get the line (up to $maxBytes bytes) at the current position, or $pos fgets()
readBytes(int $length, int $pos = null) ?string Get $length bytes from the current position, or $pos fread()
write(string $data, int $maxBytes = null, int $pos = null) int Write (up to $maxBytes bytes from) $data to the current position, or $pos fputs()
truncate(int $size = 0) void Truncate the file to $size bytes ftruncate()
flush() void Write all data to disk fflush()
lockShared(bool $nonBlocking = false) void Lock the file in shared mode flock()
lockExclusive(bool $nonBlocking = false) void Lock the file in exclusive mode flock()
unlock(bool $nonBlocking = false) void Unlock the file flock()
stat() void Get file information fstat()
isAtEOF() bool Returns true if the file pointer is at the end of the file feof()
getFileHandle(): resource void Get the file handle (e.g. to be used with other file functions) fopen() (return value)

Open File Modes

See the fopen() documentation for a description of modes.

Constant Mode Description
MODE_READ r Read-only
MODE_READWRITE r+ Read/Write
MODE_WRITE_CREATE_OR_TRUNCATE w Write-only; create file, truncate if existing
MODE_READWRITE_CREATE_OR_TRUNCATE w+ Read/Write; create file, truncate if existing
MODE_WRITE_APPEND_CREATE a Write-only; append; create file if not existing
MODE_READWRITE_APPEND_CREATE a+ Read/Write; append; create file if not existing
MODE_WRITE_CREATE_NEW x Write-only; create file, fail if existing
MODE_READWRITE_CREATE_NEW x+ Read/Write; create file, fail if existing
MODE_WRITE_CREATE c Write-only; create file, don't truncate if existing
MODE_READWRITE_CREATE c+ Read/Write; create file, don't truncate if existing

Exceptions

  • FileException
  • OpenFileErrorFileException
  • CloseFileErrorFileException
  • GetPositionErrorFileException
  • SetPositionErrorFileException
  • ReadErrorFileException
  • WriteErrorFileException
  • TruncateErrorFileException
  • FlushErrorFileException
  • ValueErrorFileException
  • LockWouldBlockFileException

统计信息

  • 总下载量: 10
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 0
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: Apache-2.0
  • 更新时间: 2021-12-29

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固