thinkphp6 composer安装think-filesystem
时间: 2024-02-23 13:00:01 浏览: 23
在 ThinkPHP 6 中使用 `think-filesystem`,可以通过 Composer 来安装。具体步骤如下:
1. 打开命令行工具,进入你的 ThinkPHP 6 项目根目录。
2. 运行命令 `composer require topthink/think-filesystem` 来安装 `think-filesystem`。
3. 在 `config/filesystem.php` 文件中配置你想要使用的文件系统。例如,如果你想使用本地文件系统,可以添加以下配置:
```php
<?php
return [
'default' => 'local',
'disks' => [
'local' => [
'type' => 'local',
'root' => app()->getRuntimePath() . 'storage',
],
],
];
```
这里我们使用了 `app()->getRuntimePath() . 'storage'` 作为本地文件系统的根目录。
4. 在你的 PHP 代码中使用 `think-filesystem`,例如:
```php
<?php
use think\facade\Filesystem;
// 写入文件
Filesystem::put('file.txt', 'Hello World!');
// 读取文件
$content = Filesystem::read('file.txt');
echo $content;
?>
```
这样就可以使用 `think-filesystem` 来读写文件了。
请注意,`think-filesystem` 已经封装了 `FileSystem`,因此我们无需手动初始化 `FileSystem`,直接使用 `think\facade\Filesystem` 即可。另外,由于 ThinkPHP 6 默认采用 Composer 自动加载,因此无需手动引入类文件。