Move Files Around | Lumen 8.0
If you are using Lumen there is a good chance that you are using it for your mobile app. Maybe that's just me. Whatever the reason is moving files from sub-domain to parent -or another subdomain- is kinda tricky with Lumen. Laravel supports moving files from/to other folders. The only reason Lumen doesn't, it doesn't have a package called filesystem installed so let's get started by installing it.
composer require "league/flysystem: ~1.0"
We are forcing v1 because v2 doesn't work with Lumen.
Time to let Lumen know about the filesystem. We can do it in bootstrap/app.php
.
First you need to uncomment $app->withFacades();
if you haven't already.
Find the Register Container Bindings section and add;
$app->singleton('filesystem', function ($app) {
return $app->loadComponent('filesystems', 'Illuminate\Filesystem\FilesystemServiceProvider', 'filesystem');
});
Now we need our config file but don't worry we can get a little help from our big brother for that. Copy config file from here and place it under config folder in the root dictionary (config/filesystems.php
). If it doesn't exist you can create it.
By editing the config file you can tell Lumen the location of your parent/sub-domain. Let's add our images folder under the domain's root as a disk.
'disks' => [
/* your other disks */
'images' => [
'driver' => 'local',
'root' => '/home/username/domains/domain.com/public_html/images',
'visibility' => 'public',
],
],
Also, since our disks have an absolute path, you might want to remove Symbolic Links by emptying the links
array or comment out the only line like below.
'links' => [
// public_path('storage') => storage_path('app/public'),
]
Configuration is done now we can actually save some files to our images disk.
if($request->hasFile('uploaded'){
$uploaded_image = $request->file.('uploaded'); // Remove the dot
$uploaded_image_filename = $uploaded_image->getClientOriginalName();
$new_file_name = time() . '_' .$uploaded_image_filename;
// First argument is a directory name
// 2nd argument is our new file name
// 3rd argument is the disk name we just created
$uploaded_image->storeAs.('profile', $new_file_name, 'images'); // Remove the dot
}
Our file will be saved under images/profile
.