folded/文件

使用针对您的Web应用的函数操作文件。

v0.2.0 2020-10-11 09:10 UTC

This package is auto-updated.

Last update: 2024-09-17 16:14:44 UTC


README

使用针对您的Web应用的函数操作文件。

Build Status Maintainability TODOs

摘要

关于

我创建了这个库,以避免在代码中使用文件函数时抛出异常。

Folded是一个由包组成的星座,帮助您轻松设置Web应用,使用即插即用的包。

特性

  • 如果在使用函数时发生错误,将抛出异常
  • 与原生函数具有完全相同的签名

需求

  • PHP版本 >= 7.4.0
  • 已安装Composer

安装

在您的根目录中,运行此命令

composer required folded/file

示例

1. 打开文件

在此示例中,我们将获取一个打开的文件。

use function Folded\openFile;

$file = openFile("path/to/file.txt", "r");

2. 关闭文件

在此示例中,我们将关闭一个打开的文件。

use function Folded\openFile;
use function Folded\closeFile;

$file = openFile("path/to/file.txt", "r");

closeFile($file);

3. 删除文件

在此示例中,我们将删除一个现有文件。

use function Folded\deleteFile;

deleteFile("path/to/file.txt");

4. 将CSV行写入文件

在此示例中,我们将向文件中写入CSV行。

use function Folded\openFile;
use function Folded\addCsvRowToFile;

$file = openFile("path/to/file.csv", "w");

addCsvRowToFile($file, ["foo", "bar"]);

5. 从文件读取CSV行

在此示例中,我们将从文件中获取一个CSV行。后续调用将获取文件中的下一行。

use function Folded\openFile;
use function Folded\getCsvRowFromFile;

$file = openFile("path/to/file.csv", "r");

$firstRow = getCsvRowFromFile($file);
$secondRow = getCsvRowFromFile($file);

6. 重命名文件

在此示例中,我们将重命名一个文件。

use function Folded\changeName;

changeName("path/to/old.txt", "path/to/new.txt");

7. 在文件上写入

在此示例中,我们将向一个打开的文件中写入。

use function Folded\writeOnFile;

$file = fopen("path/to/file.txt");

writeToFile($file, "some text");

如果您需要获取写入的字节数,请获取函数的返回值。

use function Folded\writeOnFile;

$file = fopen("path/to/file.txt");

$numberOfBytesWritten = writeToFile($file, "some text");

echo "$numberOfBytesWritten bytes written";

版本支持