File storage
Introductionβ
BowPHP includes a file management system that lets you manipulate files with great simplicity.
Configurationβ
The configuration file for the file management system is located at config/resource.php.
How it worksβ
You can either use services or work with your local disk. The static methods local and service let you manipulate the disk system and the storage services respectively, currently ftp and s3.
Local file systemβ
To manage your local file system with BowPHP, you'll use the static method local.
Explaining the conceptβ
We'll call the various specified folders disk.
Consider the following configuration:
'disk' =>[
'mount' => 'storage',
'path' => [
'storage' => __DIR__.'/../var/app',
'public' => __DIR__.'/../public',
]
];
You can specify as many disks as you want.
The Storage class exposes the static method local, which allows file manipulation. The value passed to local must be a valid disk name specified in the path list.
use Bow\Storage\Storage;
Storage::local('public'); // named disk
Storage::local(); // default disk (`mount` key)
// `Storage::disk()` is an alias of `Storage::local()`
Storage::disk('public');
If the local method (or its alias disk) is called without a parameter, it
will default to the value of the mount key.
You can also use the app_storage() helper, which works in exactly the same way:
app_storage('public');
// Or
app_storage()->get('app.js');
The S3 and FTP servicesβ
To use an external storage system, you do so via the service method. This method lets you select the type of external storage you want to use. You'll find the necessary service configuration in the configuration file, in the services section of config/resource.php.
Example with the ftp service:
$service = Storage::service('ftp');
$service->get('app.js');
Manipulating filesβ
This section describes the various methods available for manipulating files and how to use them.
Let's assume we have the file app.txt in the public disk:
// content of the app.txt file
Hello, world
Note that these methods (except the get method) return true or false respectively on success or failure.
Retrieving the content of a fileβ
The get method is used to retrieve the content of a file. It takes the file name as a parameter.
$local_storage = app_storage('public');
$content = $local_storage->get("app.txt");
// With a service
$service = Storage::service('ftp');
$content = $service->get("app.txt");
echo $content;
// => Hello, world
service and local use the same manipulation interface. This means that the methods available for local also exist for service.
Adding/Modifying the content of a fileβ
The put method lets you add to or modify the content of a file:
$local_storage = app_storage('public');
$content = 'console.log("Hello, world")';
$local_storage->put('app.js', $content );
Adding content to the beginning or end of a fileβ
The prepend and append methods let you add content to the beginning or end of a file respectively:
$local_storage = app_storage('public');
$local_storage->prepend('app.txt', 'Content added at the beginning');
$local_storage->append('app.txt', 'Content added at the end');
Deleting a fileβ
The delete method lets you delete a file.
$local_storage = app_storage('public');
$local_storage->delete('app.js');
Copying or moving a fileβ
$local_storage = app_storage('public');
$local_storage->copy('app.txt', 'sous-dossier/app.txt');
$local_storage->move('app.txt', 'archive/app.txt'); // copy + delete the source
Listing files and foldersβ
To enumerate the contents of a folder:
$local_storage = app_storage('public');
$files = $local_storage->files('uploads'); // files in a folder
$subdirs = $local_storage->directories('uploads'); // subfolders
Storing an uploaded fileβ
To persist a file coming from $request->file(...):
$file = request()->file('avatar');
$path = app_storage('public')->store($file, 'avatars');
// path: relative path of the stored file
Creating a folderβ
You can create a new folder using the makeDirectory method:
Prototypeβ
makeDirectory(string $dirname, int $mode = 0777): bool
| Parameter | Type | Description |
|---|---|---|
dirname | string | The path of the folder to create |
mode | int | UNIX mode, defaults to 0777 |
Bow calls mkdir($dirname, $mode, true) internally β recursive
creation is always enabled. There's no need for a dedicated flag to
create a tree like chemin/vers/un/dossier.
$local_storage = app_storage('public');
$local_storage->makeDirectory('dossier');
$local_storage->makeDirectory('chemin/vers/un/dossier'); // recursive
$local_storage->makeDirectory('private', 0750); // custom mode
Checking whether a file existsβ
$local_storage = app_storage('public');
if ($local_storage->exists('app.txt')) {
echo $local_storage->get('app.txt');
}