File storage in BowPHP
- Introduction
- Configuration
- How it works
- Working with files
- Retrieving the content of a file
- Adding/Modifying the content of a file
- Adding content at the beginning or end of a file
- Deleting a file
- Copying a file
- Creating a directory
- Checking whether a file exists
- Checking whether the parameter is a file
- Checking whether the parameter is a directory
- Getting the absolute path of a file or directory
Introductionβ
BowPHP includes a file management system that lets you work with files with great simplicity.
Configurationβ
The configuration file for the file management system is located at config/resource.php.
How it worksβ
You can use the services or work on your local disk. The static methods mount and service respectively let you work with the disk system and with the storage services, currently ftp and s3.
Local file systemβ
To manage your local file system with BowPHP, you will use the static method mount.
Explaining the conceptβ
We will refer to the different specified directories as 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 mount, which allows file manipulation. The value passed to mount must be a valid disk name specified in the path list.
use Bow\Storage\Storage;
Storage::mount("public");
// Or
Storage::mount();
If the
mountmethod is called without a parameter, it will use the value of themountkey by default.
You can also use the mount() helper, which works exactly the same way:
mount('public');
// Or
mount()->get('app.js');
The S3 and FTP servicesβ
To use an external storage system, you do so through the service method. This method lets you select the type of external storage you want to use. You will find the required service configuration in the services section of the config/resource.php file.
Example with the ftp service:
$service = Storage::service('ftp');
$service->get('app.js');
Working with filesβ
This section describes the different methods available for working with files and how to use them.
Let's assume we have the file app.txt on the public disk:
// content of the app.txt file
Hello, world
Note that these methods (except the
getmethod) returntrueorfalserespectively 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 its parameter.
$mount = mount('public');
$content = $mount->get("app.txt");
// With a service
$service = Storage::service('ftp');
$content = $service->get("app.txt");
echo $content;
// => Hello, world
serviceandmountuse the same manipulation interface. This means that the methods available formountalso exist forservice.
Adding/Modifying the content of a fileβ
The put method lets you add or modify the content of a file:
$mount = mount('public');
$content = 'console.log("Hello, world")';
$mount->put('app.js', $content );
Adding content at the beginning or end of a fileβ
The preprend and append methods let you add content at the beginning or end of a file respectively:
$mount = mount('public');
$mount->prepend('app.txt', 'Content added at the beginning');
$mount->append('app.txt', 'Content added at the end');
Deleting a fileβ
The delete method lets you delete a file.
$mount = mount('public');
$mount->delete('app.js');
Copying a fileβ
$mount = mount('public');
$mount->copy('app.txt', 'sous-dossier/app.txt');
Creating a directoryβ
You can create a new directory using the makeDirectory method:
Prototypeβ
$mount = mount('public');
$mount->makeDirectory($dirname, $mode = 0777, $recursive = false);
| Parameter | Type |
|---|---|
| path | String - the directory to create |
| mode | Int - The directory mode, default 777 |
| recursive | Boolean - Allows recursive creation |
$mount = mount('public');
$mount->makeDirectory('dossier');
Note that you can change the directory mode and choose to create it recursively.
$mount->makeDirectory('chemin/vers/un/dossier', 777, true);
Checking whether a file existsβ
$mount = mount('public');
if ($mount->exists('app.txt')) {
echo $mount->get('app.txt');
}
Checking whether the parameter is a fileβ
$mount = mount('public');
if ($mount->isFile('app.txt')) {
echo "It's a file";
}
Checking whether the parameter is a directoryβ
$mount = mount('public');
if ($mount->isDirectory('nom_du_dossier')) {
echo "It's a directory";
}
Getting the absolute path of a file or directoryβ
$mount = mount('public');
$path = $mount->path('app.txt');
echo $path;
// => /chemin/absolu/vers/le/fichier/app.txt
Is something missing?
If you run into problems with the documentation or have suggestions to improve the documentation or the project in general, please open an issue for us, or send a tweet mentioning the Twitter account @bowframework or directly on github.