PHPackages                             luchavez/simple-files - 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. luchavez/simple-files

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

luchavez/simple-files
=====================

A simple approach to adding polymorphic files such as images to any Eloquent models in Laravel 9|10

1.4.1(2y ago)11.6kMITPHPPHP ^8.0

Since Mar 26Pushed 2y ago1 watchersCompare

[ Source](https://github.com/luchavez-technologies/simple-files)[ Packagist](https://packagist.org/packages/luchavez/simple-files)[ Docs](https://github.com/luchavez-technologies/simple-files)[ RSS](/packages/luchavez-simple-files/feed)WikiDiscussions develop Synced today

READMEChangelogDependencies (8)Versions (25)Used By (0)

Simple Files for Laravel 9|10
=============================

[](#simple-files-for-laravel-910)

[![Latest Version on Packagist](https://camo.githubusercontent.com/7d36d7e4fff0ae23d0eaa26a1692c41a5552a5a6dd5540f6738d1472270ca7a1/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f6c7563686176657a2f73696d706c652d66696c65732e737667)](https://packagist.org/packages/luchavez/simple-files)[![Total Downloads](https://camo.githubusercontent.com/2a08b7370d0c0f63361f534925800bba78d1f57df735ba1940b8dcccebff0dbc/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f6c7563686176657a2f73696d706c652d66696c65732e737667)](https://packagist.org/packages/luchavez/simple-files)[![GitHub Repo stars](https://camo.githubusercontent.com/ad3af5feb68f36a4ca29b6577249e9753d220a007db249d5e698321cc145ee43/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f73746172732f6c7563686176657a2d746563686e6f6c6f676965732f73696d706c652d66696c6573)](https://github.com/luchavez-technologies/simple-files)[![Discord](https://camo.githubusercontent.com/bd46299a29c3cae288e34846117d44d0cb853da2dad0c376fd8bca3fc269f3b6/68747470733a2f2f696d672e736869656c64732e696f2f646973636f72642f313134333734343631393935363430343239353f636f6c6f723d386339656666266c6162656c3d446973636f7264266c6f676f3d646973636f7264)](https://discord.gg/MBxxAkQAxx)[![Twitter Follow](https://camo.githubusercontent.com/516380eada3f11aaafd758d9d0b03958251eca9097ba558c3ae9ff53ee59820a/68747470733a2f2f696d672e736869656c64732e696f2f747769747465722f666f6c6c6f772f6c7563686176657a74656368)](https://twitter.com/luchaveztech)

[![Simple Files Banner](./images/banner.png "Banner")](./images/banner.png)

Introduction
------------

[](#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!

[![](./images/logo.png)](./images/logo.png)

Check out the [examples](#examples) below to get a clearer picture.

Installation
------------

[](#installation)

Via Composer

```
$ composer require luchavez/simple-files:^1.0.0
```

Setting Up
----------

[](#setting-up)

1. Add the [HasFilesTrait](./src/Traits/HasFilesTrait.php) trait to all the Eloquent models that can own files.
2. Run the migrations to add the [**files**](./database/migrations/2022_03_30_000000_create_files_table.php) and [**fileables**](./database/migrations/2022_03_30_000002_create_fileables_table.php) tables.
3. Add these variables to `.env` file if you want to override the default values.

Variable NameDefault Value`SF_FILESYSTEM_DISK``config('filesystems.default')``SF_EXPIRE_AFTER``1 day``SF_PUBLIC_DIRECTORY``public``SF_PRIVATE_DIRECTORY``private`Usage
-----

[](#usage)

### SimpleFiles

[](#simplefiles)

The package provides a service called [**SimpleFiles**](src/Services/SimpleFiles.php) which you can use by calling its [helper functions](helpers/simple-files-helper.php):

1. `simpleFiles()`
2. `simple_files()`

Here's the list of its available methods.

Method NameReturn TypeDescription`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

[](#hasfilestrait)

The package also provides [**HasFilesTrait**](src/Traits/HasFilesTrait.php) 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 NameReturn TypeDescription`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 modelExamples
--------

[](#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

[](#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](https://www.postman.com/) to upload files to the route above.

- Here's an example of normal `FormData` file upload using `storePublicly()`.

[![img.png](images/public-formdata.png)](images/public-formdata.png)

- Here's an example of normal `FormData` file upload using `storePrivately()`.

[![img.png](images/private-formdata.png)](images/private-formdata.png)

- Here's an example of `Base64` encoded file upload using `storePublicly()`.

[![img.png](images/public-base64.png)](images/public-base64.png)

- Here's an example of `Base64` encoded file upload using `storePrivately()`.

[![img.png](images/private-base64.png)](images/private-base64.png)

- Here's an example of file upload from `URL` using `storePublicly()`.

[![img.png](images/public-url.png)](images/public-url.png)

- Here's an example of file upload from `URL` using `storePrivately()`.

[![img.png](images/private-url.png)](images/private-url.png)

**Note**: If you will open an expired url, you'll receive a `Request has expired` error.

[![img.png](images/expired-url.png)](images/expired-url.png)

### Attaching files

[](#attaching-files)

To attach an uploaded file to a model, use the `attachFiles()` method from [**HasFilesTrait**](#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
----------

[](#change-log)

Please see the [changelog](changelog.md) for more information on what has changed recently.

Testing
-------

[](#testing)

```
$ composer test
```

Contributing
------------

[](#contributing)

Please see [contributing.md](contributing.md) for details and a todolist.

Security
--------

[](#security)

If you discover any security related issues, please email  instead of using the issue tracker.

Credits
-------

[](#credits)

- [James Carlo S. Luchavez](https://github.com/luchavez-technologies)
- [All Contributors](../../contributors)

License
-------

[](#license)

MIT. Please see the [license file](license.md) for more information.

###  Health Score

29

—

LowBetter than 57% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity17

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity60

Established project with proven stability

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~17 days

Recently: every ~4 days

Total

20

Last Release

871d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/9751834583538187236ea6491879aa80263a43dd036aa4e4fd0a71f721a0bed4?d=identicon)[jamescarloluchavez](/maintainers/jamescarloluchavez)

---

Top Contributors

[![luchtech](https://avatars.githubusercontent.com/u/26067874?v=4)](https://github.com/luchtech "luchtech (29 commits)")

---

Tags

laravelSimpleFiles

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/luchavez-simple-files/health.svg)

```
[![Health](https://phpackages.com/badges/luchavez-simple-files/health.svg)](https://phpackages.com/packages/luchavez-simple-files)
```

###  Alternatives

[unisharp/laravel-filemanager

A file upload/editor intended for use with Laravel 5 to 10 and CKEditor / TinyMCE

2.2k3.5M85](/packages/unisharp-laravel-filemanager)[psalm/plugin-laravel

Psalm plugin for Laravel

3355.3M345](/packages/psalm-plugin-laravel)[laravel/ai

The official AI SDK for Laravel.

1.0k3.2M194](/packages/laravel-ai)[api-platform/laravel

API Platform support for Laravel

58171.5k14](/packages/api-platform-laravel)[ublabs/blade-simple-icons

A package to easily make use of Simple Icons in your Laravel Blade views.

1963.4k](/packages/ublabs-blade-simple-icons)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
