luchavez/simple-files
Composer 安装命令:
composer require luchavez/simple-files
包简介
A simple approach to adding polymorphic files such as images to any Eloquent models in Laravel 9|10
关键字:
README 文档
README
Introduction
Implementing a public-private filesystem structure to your Laravel application can be a hassle. But with "luchavez/simple-files", it's easy! This package is built with that in mind so that you can proceed with more important things in your project. It's perfect for Laravel applications that need to work with files. And the best part? It can process not only uploaded files but also Base64 strings and image URLs! Plus, it's especially effective on AWS S3 buckets. Try it out today and see how it can help you!
Check out the examples below to get a clearer picture.
Installation
Via Composer
$ composer require luchavez/simple-files:^1.0.0
Setting Up
-
Add the HasFilesTrait trait to all the Eloquent models that can own files.
-
Add these variables to
.envfile if you want to override the default values.
| Variable Name | Default Value |
|---|---|
SF_FILESYSTEM_DISK |
config('filesystems.default') |
SF_EXPIRE_AFTER |
1 day |
SF_PUBLIC_DIRECTORY |
public |
SF_PRIVATE_DIRECTORY |
private |
Usage
SimpleFiles
The package provides a service called SimpleFiles which you can use by calling its helper functions:
simpleFiles()simple_files()
Here's the list of its available methods.
| Method Name | Return Type | Description |
|---|---|---|
getFileSystemDisk |
string |
gets the filesystem disk |
getDisk |
string |
shortcut for getFileSystemDisk method |
getExpireAfterUnit |
string |
gets the time unit for temporary URL expiration |
getExpireAfterValue |
string |
gets the time value for temporary URL expiration |
getExpireAfter |
Illuminate\Support\Carbon |
gets the Carbon equivalent of getExpireAfterUnit() and getExpireAfterValue() |
getPublicDirectory |
string |
gets the specified public directory |
getPrivateDirectory |
string |
gets the specified private directory |
shouldOverwriteOnExists |
bool |
decides whether to overwrite or not when it already exists |
getFileSystemAdapter |
Illuminate\Contracts\Filesystem\Filesystem |
gets the filesystem adapter |
getAdapter |
Illuminate\Contracts\Filesystem\Filesystem |
shortcut for getFileSystemAdapter method |
getPublicAdapter |
Illuminate\Contracts\Filesystem\Filesystem |
uses getFileSystemAdapter method with $is_public to true |
getPrivateAdapter |
Illuminate\Contracts\Filesystem\Filesystem |
uses getFileSystemAdapter method with $is_public to false |
store |
Luchavez\SimpleFiles\Models\File or Collection or null |
stores file/s to specified public or private directory |
storePublicly |
Luchavez\SimpleFiles\Models\File or Collection or null |
uses store() method with $is_public to true |
storePrivately |
Luchavez\SimpleFiles\Models\File or Collection or null |
uses store() method with $is_public to false |
getContentsFromURL |
string or null |
gets contents by using file_get_contents() |
getContentsFromBase64 |
string or null |
gets contents by using base64_decode() |
getFiles |
array |
gets files list from specified public or private directory |
getPublicFiles |
array |
uses getFiles() method with $is_public to true |
getPrivateFiles |
array |
uses getFiles() method with $is_public to false |
getFile |
string or null |
gets file contents |
getFilePublicly |
string or null |
uses getFile() method with $is_public to true |
getFilePrivately |
string or null |
uses getFile() method with $is_public to false |
putFile |
string or null |
uploads file to specified public or private directory |
putFilePublicly |
string or null |
uses putFile() method with $is_public to true |
putFilePrivately |
string or null |
uses putFile() method with $is_public to false |
putFileAs |
string or null |
uploads file to specified public or private directory |
putFilePubliclyAs |
string or null |
uses putFileAs() method with $is_public to true |
putFilePrivatelyAs |
string or null |
uses putFileAs() method with $is_public to false |
exists |
bool |
checks if file exists on specified public or private directory |
existsPublicly |
bool |
uses exists() method with $is_public to true |
existsPrivately |
bool |
uses exists() method with $is_public to false |
delete |
bool |
deletes file/s from specified public or private directory |
deletePublicly |
bool |
uses delete() method with $is_public to true |
deletePrivately |
bool |
uses delete() method with $is_public to false |
getDirectories |
array |
gets files list from specified public or private directory |
getPublicDirectories |
array |
uses getDirectories() method with $is_public to true |
getPrivateDirectories |
array |
uses getDirectories() method with $is_public to false |
deleteFiles |
bool |
deletes files from specified public or private directory |
deletePublicFiles |
bool |
uses deleteFiles() method with $is_public to true |
deletePrivateFiles |
bool |
uses deleteFiles() method with $is_public to false |
relateFileModelTo |
void |
dynamically build relationship from any model to the File model |
generateUrl |
void |
create a new url or temporary url to a Luchavez\SimpleFiles\Models\File instance |
HasFilesTrait
The package also provides HasFilesTrait which you can use on Eloquent models that you want to have files or images.
- If a User model needs profile pictures...
use Luchavez\SimpleFiles\Traits\HasFilesTrait;
class User extends Authenticatable
{
use HasFactory, Notifiable, HasFilesTrait;
...
- Or, if a Company model needs logos...
use Luchavez\SimpleFiles\Traits\HasFilesTrait;
class Company extends Model
{
use HasFactory, HasFilesTrait;
...
- Or, if a Food model needs some Instagram-worthy images...
use Luchavez\SimpleFiles\Traits\HasFilesTrait;
class Food extends Model
{
use HasFactory, HasFilesTrait;
...
Here's the list of methods that will be added to Eloquent models.
| Method Name | Return Type | Description |
|---|---|---|
files |
Illuminate\Database\Eloquent\Relations\MorphToMany |
gets all files |
images |
Illuminate\Database\Eloquent\Relations\MorphToMany |
gets all image files |
nonImages |
Illuminate\Database\Eloquent\Relations\MorphToMany |
gets all non-image files |
fileables |
Illuminate\Database\Eloquent\Relations\MorphMany |
gets all fileables |
fileable |
Illuminate\Database\Eloquent\Relations\MorphOne |
gets latest fileable |
imageables |
Illuminate\Database\Eloquent\Relations\MorphMany |
gets all imageables |
imageable |
Illuminate\Database\Eloquent\Relations\MorphOne |
gets latest imageable |
nonImageables |
Illuminate\Database\Eloquent\Relations\MorphMany |
gets all non-imageables |
nonImageable |
Illuminate\Database\Eloquent\Relations\MorphOne |
gets latest non-imageable |
attachFiles |
void |
attaches file/s to model |
attachPublicFiles |
void |
attaches file/s to model |
attachPrivateFiles |
void |
attaches file/s to model |
syncFiles |
void |
syncs file/s to model |
syncPublicFiles |
void |
syncs file/s to model |
syncPrivateFiles |
void |
syncs file/s to model |
detachFiles |
void |
detaches file/s to model |
detachPublicFiles |
void |
detaches file/s to model |
detachPrivateFiles |
void |
detaches file/s to model |
Examples
Note: In case you did not specify the user who uploaded the file, it will try to get the authenticated user via auth()->user().
Here are the .env variables used in this example:
SF_FILESYSTEM_DISK=s3 SF_PUBLIC_DIRECTORY=public/dev/dummy SF_PRIVATE_DIRECTORY=private/dev/dummy SF_EXPIRE_AFTER=1 day
Uploading files
Let's start first by creating a public route /api/files. Use storePublicly() and storePrivately() methods of simpleFiles() global helper function to store the files on public/dev/dummy and private/dev/dummy directories respectively.
- Using
storePublicly()with$request->user()as uploader.
Route::post('/files', function (Request $request) {
$file = $request->file;
$user = $request->user();
return simpleFiles()->storePublicly(file: $file, user: $user)->toArray();
});
- Using
storePrivately()with$request->user()as uploader.
Route::post('/files', function (Request $request) {
$file = $request->file;
$user = $request->user();
return simpleFiles()->storePrivately(file: $file, user: $user)->toArray();
});
Once that is set up, we can use Postman to upload files to the route above.
- Here's an example of normal
FormDatafile upload usingstorePublicly().
- Here's an example of normal
FormDatafile upload usingstorePrivately().
- Here's an example of
Base64encoded file upload usingstorePublicly().
- Here's an example of
Base64encoded file upload usingstorePrivately().
- Here's an example of file upload from
URLusingstorePublicly().
- Here's an example of file upload from
URLusingstorePrivately().
Note: If you will open an expired url, you'll receive a Request has expired error.
Attaching files
To attach an uploaded file to a model, use the attachFiles() method from HasFilesTrait.
- If a User model needs profile pictures...
Route::post('upload-files', function (Request $request) {
$user = \App\Models\User::query()->first();
$for_upload = $request->file;
// You can attach like this...
$file = simpleFiles()->storePublicly(file: $for_upload);
$user->attachFiles(file: $file);
// Or using a one-liner...
$user->attachPublicFiles(file: $for_upload)
return simpleResponse()->message('Successfully attached files.')->generate();
});
- Or, if a Company model needs logos...
Route::post('upload-files', function (Request $request) {
$company = \App\Models\Company::query()->first();
$for_upload = $request->file;
// You can attach like this...
$file = simpleFiles()->storePublicly(file: $for_upload);
$company->attachFiles(file: $file);
// Or using a one-liner...
$company->attachPublicFiles(file: $for_upload)
return simpleResponse()->message('Successfully attached files.')->generate();
});
- Or, if a Food model needs some Instagram-worthy images...
Route::post('upload-files', function (Request $request) {
$food = \App\Models\Food::query()->first();
$for_upload = $request->file;
// You can attach like this...
$file = simpleFiles()->storePublicly(file: $for_upload);
$food->attachFiles(file: $file);
// Or using a one-liner...
$food->attachPublicFiles(file: $for_upload)
return simpleResponse()->message('Successfully attached files.')->generate();
});
Change log
Please see the changelog for more information on what has changed recently.
Testing
$ composer test
Contributing
Please see contributing.md for details and a todolist.
Security
If you discover any security related issues, please email author@email.com instead of using the issue tracker.
Credits
License
MIT. Please see the license file for more information.
luchavez/simple-files 适用场景与选型建议
luchavez/simple-files 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 1.58k 次下载、GitHub Stars 达 0, 最近一次更新时间为 2023 年 03 月 21 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「laravel」 「SimpleFiles」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 luchavez/simple-files 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 luchavez/simple-files 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 luchavez/simple-files 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Alfabank REST API integration
Laravel package for Accurate Online API integration.
Shared RCX Laravel DataTables UI and configuration helpers.
Boot a Laravel project on any machine with one command: app:serve installs missing tools (PHP, Node, Composer, Herd, Docker), creates .env, sets up the database, runs migrations, builds assets, starts a queue worker and serves via Herd, Sail or artisan serve; app:down cleanly stops everything it sta
Branded, diagnostic error pages (500, 403, 404, 419, 503) for Filament — native Filament UI, dark mode and translations out of the box.
Turn any PDF into a Pingen-ready A4 letter (generated address cover page + A4 normalisation + safe margins) and send it through the Pingen print & mail API. Laravel-first.
统计信息
- 总下载量: 1.58k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 10
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2023-03-21








