bitandblack/base64-string

将文件编码为base64字符串,并将base64字符串解码为文件。

1.1.0 2024-01-30 07:44 UTC

This package is auto-updated.

Last update: 2024-08-30 01:14:45 UTC


README

PHP from Packagist Codacy Badge Latest Stable Version Total Downloads License

Bit&Black Base64 String

将文件编码为base64字符串,并将base64字符串解码为文件。

安装

此库适用于与 Composer 一起使用。通过运行 $ composer require bitandblack/base64-string 将其添加到项目中。

用法

使用要编码的文件的路径初始化 Base64String

<?php

use BitAndBlack\Base64String\Base64String;

$base64String = new Base64String('/path/to/myfile.txt');

现在您可以像这样访问base64字符串的各个部分

<?php

/** Will echo something like "data:text/plain;base64,SGVsbG8gV29ybGQh..." */
echo $base64String->getBase64String();

/** Will echo "text/plain" */
echo $base64String->getMimeType();

或者简单地echo对象

<?php

/** This will also echo something like "data:text/plain;base64,SGVsbG8gV29ybGQh..." */
echo new Base64String('/path/to/myfile.txt');

反向

您还可以将base64编码的字符串转换为文件并提取其部分

<?php

use BitAndBlack\Base64String\Base64File;

$base64File = new Base64File('data:text/plain;base64,SGVsbG8gV29ybGQh');

现在您可以像这样访问文件的各个部分

<?php

/** Will echo "txt" */
echo $base64File->getExtension();

/** Will echo "Hello World!" using the input string above */
echo $base64File->getContent();

/** Will echo "text/plain" */
echo $base64File->getMimeType();

有了这些信息,很容易将该文件写入文件系统

<?php

file_put_contents(
    'myfile.txt', /** Or `'myfile.' . $base64File->getExtension(),` if you prefer. */
    $base64File
);

帮助

如果您有任何问题,请随时通过 hello@bitandblack.com 联系我们。

有关Bit&Black的更多信息,请访问 www.bitandblack.com