PHPackages                             lfyw/file-manager - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [File &amp; Storage](/categories/file-storage)
4. /
5. lfyw/file-manager

ActiveLibrary[File &amp; Storage](/categories/file-storage)

lfyw/file-manager
=================

A file manager

4.2.3(2y ago)122944MITPHPPHP ^7.3|^8.0

Since Mar 15Pushed 2y ago1 watchersCompare

[ Source](https://github.com/lfyw/file-manager)[ Packagist](https://packagist.org/packages/lfyw/file-manager)[ RSS](/packages/lfyw-file-manager/feed)WikiDiscussions master Synced today

READMEChangelog (10)DependenciesVersions (47)Used By (0)

 file-manager
==============

[](#-file-manager-)

 A file manager.

安装
--

[](#安装)

```
$ composer require lfyw/file-manager
```

使用
--

[](#使用)

### 数据库迁移

[](#数据库迁移)

执行数据库迁移:

```
$ php artisan migrate
```

如果需要对数据表做修改，可以导出迁移文件:

```
$ php artisan vendor:publish --tag='migrations'
```

### 配置

[](#配置)

导出配置文件:

```
$ php artisan vendor:publish --tag='config'
```

`path`是文件存放目录；`clear_sync_file`是指同步完文件，是否删除同步中失效的文件，建议`true`

```
return [
    'path' => env('FILE_PATH', 'public/uploads'),
    'clear_sync_file' => env('FILE_CLEAR', true)
];
```

如果需要给模型设置另外的链接，则可以增加`connection`属性，会读取`database.connections`中对应的连接配置。

```
return [
    'path' => env('FILE_PATH', 'public/uploads'),
    'clear_sync_file' => env('FILE_CLEAR', true),
    'connection' => 'test'
];
```

### 文件上传

[](#文件上传)

**upload($file, $keepOriginalName = false, $guessExtension = true)**

像下面这样来上传文件。第一个参数是上传的文件;第二个参数是上传时是否以原文件名进行保存，默认会重新命名;第三个参数是是否根据文件 MIME 类型推测文件后缀，默认`true`, 如果要保存文件的原后缀名请改为`false`

```
class FilesController extends Controller
{
    public function store(Request $request)
    {
        return \Lfyw\FileManager\Models\File::upload($request->file('file'), $keepOriginalName = false, $guessExtension = true);
    }
}
```

结果会返回一个`File`模型:

```
{
    "original_name": "卡佐科技开放平台接入协议V1.5.docx",
    "save_name": "nxSwybO01e6hLooUIS2ClOzxhV1Mhw4easqx2guz.docx",
    "path": "uploads/nxSwybO01e6hLooUIS2ClOzxhV1Mhw4easqx2guz.docx",
    "url": "/storage/uploads/nxSwybO01e6hLooUIS2ClOzxhV1Mhw4easqx2guz.docx",
    "extension": "docx",
    "extra": {
        "client_extension": "docx",
        "clientMineType": "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
        "extension": "docx",
        "size": 34003
    },
    "updated_at": "2020-09-28T13:16:56.000000Z",
    "created_at": "2020-09-28T13:16:56.000000Z",
    "id": 17
}
```

### 文件关联

[](#文件关联)

在目标模型文件中引用`HasFiles`trait

```
