Introduction
A framework agnostic filesystem package with multiple adapters like AwsS3 and Dropbox.
The package requires PHP 7.2+ and comes bundled with a Laravel 6 Facade and a Service Provider to simplify the optional framework integration and follows the FIG standard PSR-4 to ensure a high level of interoperability between shared PHP code and is fully unit-tested.
Have a read through the Installation Guide and on how to Integrate it with Laravel 6.
Create a new file
Filesystem::write('filename.txt', 'Your file contents');
Create a new file on your Dropbox
Filesystem::connection('dropbox')->write('filename.txt', 'Your file contents');
Installation
The best and easiest way to install the Filesystem package is with Composer.
Preparation
Open your composer.json
file and add the following to the require
array:
"cartalyst/filesystem": "^4.0"
Add the following lines after the require
array on your composer.json
file:
"repositories": [
{
"type": "composer",
"url": "https://packages.cartalyst.com"
}
]
Note: Make sure that after the required changes your
composer.json
file is valid by runningcomposer validate
.
Install the dependencies
Run Composer to install or update the new requirement.
php composer install
or
php composer update
Now you are able to require the vendor/autoload.php
file to autoload the package.
Integration
Cartalyst packages are framework agnostic and as such can be integrated easily natively or with your favorite framework.
Laravel 6
The Filesystem package has optional support for Laravel 6 and it comes bundled with a Service Provider and a Facade for easy integration.
After installing the package, open your Laravel config file located at config/app.php
and add the following lines.
In the $providers
array add the following service provider for this package.
'Cartalyst\Filesystem\Laravel\FilesystemServiceProvider',
In the $aliases
array add the following facade for this package.
'Filesystem' => 'Cartalyst\Filesystem\Laravel\Facades\Filesystem',
Configuration
After installing, you can publish the package configuration file into your application by running the following command on your terminal:
php artisan config:publish cartalyst/filesystem
This will publish the config file to config/packages/cartalyst/filesystem/config.php
where you can modify the package configuration.
Native
Integrating the package outside of a framework is incredible easy, just follow the example below.
// Include the composer autoload file
require_once 'vendor/autoload.php';
// Import the necessary classes
use Cartalyst\Filesystem\FilesystemManager;
// Require the filesystem config file
$config = require 'vendor/cartalyst/filesystem/src/config/config.php';
// Instantiate the Filesystem Manager and set the necessary configuration
$filesystem = new FilesystemManager($config);
$filesystem->setMaxFileSize(10485760); // 10 MB
$filesystem->setAllowedMimes(array(
'image/jpeg',
));
The integration is done and you can now use all the available methods, here's an example:
// Create a 'foo.txt' file
$filesystem->write('foo.txt', "Hello i'm bar and i'm awesome!");
Usage
General Usage
In this section we'll be covering the general usage of the Filesystem package.
Write a file
Filesystem::write('filename.txt', 'The file contents');
Update a file
Filesystem::update('filename.txt', 'The updated file contents');
Write or Update a file
Filesystem::put('filename.txt', 'The file contents');
Rename a file name
Filesystem::rename('filename.txt', 'foobar.txt');
Copy a file to another location
Filesystem::copy('filename.txt', '/new/path');
Read a file contents
$contents = Filesystem::read('filename.txt');
Check if a file exists
Filesystem::has('filename.txt');
Delete files
Filesystem::delete('filename.txt');
Get a file size
$size = Filesystem::getSize('filename.txt');
Upload a file
Filesystem::upload($file, 'my_uploaded_file.jpg');
Connections
The Filesystem package comes bundled with adapters that you can use to manage your files on a different storage environment.
List of available adapters:
- Local
- Amazon Web Services - S3
- Rackspace Cloud Files
- Dropbox
- Ftp
- Sftp (through phpseclib)
- Zip (through ZipArchive)
- WebDAV (through SabreDAV)
To setup your connections credentials on the config/cartalyst.filesystem.php
file.
Note: You can have as many connections you required but make sure that the connection has the
adapter
key.
The usage is very simple, please refer to the following example on how to use different connections
Filesystem::connection('dropbox')->write('filename.txt', 'File contents');
File Object
There are times you might need to get specific information about a file, we've you covered.
The very first thing you need to do, is to get a file object and to achieve this just follow the example below:
$file = Filesystem::get('filename.txt');
Now that you have your $file
object, you can use various methods, please review the examples below.
Get the file contents
$contents = $file->read();
Get the file extension
$extension = $file->getExtension();
Get the file mimetype
$mime = $file->getMimetype();
Get the file size
$size = $file->getSize();
Get the file full path
$path = $file->getFullpath();
Check if the file is an image
$isImage = $file->isImage();
Get the file width and height
$size = $file->getImageSize();
$width = $image['width'];
$height = $image['height'];
Note: This method will only work properly if the file is an image.
Delete the file
$file->delete();