Laravel File Storage Location - php

I am running my Laravel Project on centos, using the default artisan server.
I am trying to add data to files, using laravel File::append command.
In config/filesystems.php i have the following:
'disks' => [
'local' => [
'driver' => 'local',
'root' => storage_path('data'),
],
and
/*
|--------------------------------------------------------------------------
| Default Filesystem Disk
|--------------------------------------------------------------------------
|
| Here you may specify the default filesystem disk that should be used
| by the framework. The "local" disk, as well as a variety of cloud
| based disks are available to your application. Just store away!
|
*/
'default' => env('FILESYSTEM_DRIVER', 'local'),
and 'FILESYSTEM_DRIVER' is not defined in the .env file.
However when i use File::append(), the files are created in the public folder, and not storage_path
Any idea what is causing this?What am i missing.

While Storage::put('filename.jpg', $contents); uses the default filesystem and thus expects a relative filename, File::append does not.
So a possible solution here could be File::append($path, 'yourAppend'); with the full path to your file in the storage folder.

Related

disk [users] not configured, please add a disk config in `config/filesystems.php`

PHP version PHP/8.1.10
Laravel version 9.28.0
While working on a Laravel-admin backend, I tried creating a new user (basically inserting data into admin_user table in the database)
working on my windows computer, I encountered the error:
disk [users] not configured, please add a disk config in config/filesystems.php.
I revisited the Laravel docs and these are the things I did to solve the problem:
Opened the config/admin.php and added:
'disk' => 'users',
// Image and file upload path under the disk above.
'directory' => [
'image' => 'images',
'file' => 'files',
],
Then opened the config/filesystems.php and added:
'users' => [
'driver' => 'local',
'root' => storage_path('app'),
]
Closed my git bash and reopened it, ran:
php artisan serve
Accessed the page for creating a user and the error was solved..
Do you know any other way this could be solved?
To use a custom disk you must configure it first. To configure a disk, follow the following steps:
Open filesystems.php file, under config directory, in your preferred text editor or IDE.
Look for the key named disks. The array under disks is where you configure your disks. Laravel already provides some disks out of the box (I recommend not messing with them unless you know what you are doing).
Add your new disk configuration, something like:
'users' => [
'driver' => 'local',
'root' => storage_path('app/public/users'),
'url' => env('APP_URL').'/storage/users',
'visibility' => 'public',
]
Note: The above example assumes you have already executed php artisan storage:link command in order to create a symbolic link that allows the files in storage/app/public to be accessible from outside.
Save the file and now you can use that new disk like below:
use Illuminate\Support\Facades\Storage;
// initialize a new disk using the `users` disk config
$disk = Storage::disk('users');
// perform actions under that disk
$disk->delete('sub/folder/file.ext');
Also, it is important to note that you are not limited to have disks under the storage folder, you can actually have disks under the public folder:
'users' => [
'driver' => 'local',
'root' => public_path('/users'),
'url' => env('APP_URL').'/users',
'visibility' => 'public',
]
Note that the function public_path is used instead of storage_path. Also the command php artisan storage:link is not needed when your disks are under the public folder.
Learn more about File Storage in Laravel.

what is config folder and its purpose in laravel and what are these files under config folder?

What is the config folder in laravel and what is the purpose of it?? Can we imagine it as a "setting" folder? For example under this folders list of files exist . If we consider only
app.php
then what will be the purpose of a app.php? i found following code under this file
'debug' => (bool) env('APP_DEBUG', false),
'url' => env('APP_URL', 'http://localhost'),
'asset_url' => env('ASSET_URL', null),
it means if i want to access env file,i will have to access it through configuration
and i am assuming if i need to access framwork packages or libraries i will have to access through these configuration files???
Or how would you explain it to a beginner? Please give some context level defination.

InvalidArgumentException Please provide a valid cache path

As this question has been asked soo many time and the solution is clear, but my problem is I have done every possible solution but it still doesn't solve my error.
I have all the required folders
-storage
-app
-framework
-cache
-session
-testing
-views
-logs
and also all the ".gitignore" files as well
also have run permission command
Below is my config/view.php
<?php
return [
/*
|--------------------------------------------------------------------------
| View Storage Paths
|--------------------------------------------------------------------------
|
| Most templating systems load templates from disk. Here you may specify
| an array of paths that should be checked for your views. Of course
| the usual Laravel view path has already been registered for you.
|
*/
'paths' => [
resource_path('views'),
],
/*
|--------------------------------------------------------------------------
| Compiled View Path
|--------------------------------------------------------------------------
|
| This option determines where all the compiled Blade templates will be
| stored for your application. Typically, this is within the storage
| directory. However, as usual, you are free to change this value.
|
*/
'compiled' => env(
'VIEW_COMPILED_PATH',
realpath(storage_path('/storage/framework/views'))
),
];

Using storage_path from another Laravel app

I have an application that uses two different Laravel apps talking to the same database. App 1 is called BUILDER and App 2 is called VIEWER. In production I use S3 for storing files submitted within the application. For local development I use the storage/app/public folder in BUILDER.
The local dev setup is that BUILDER runs on localhost:8000 and VIEWER on locahost:8001
Now here comes my problem. In production both apps use the same S3 bucket for storage. So somehow I need to set this up similarly for local development.
The BUILDER is working fine, uploading and reading its files from the storage/app/public folder with FILESYSTEM_DRIVER=public in .env
The VIEWER is also reading these files fine, creating correct URL's after I added a new disk in the config (BUILDER_URL is set in .env to localhost:8000 which is the URL for the BUILDER)
'builder_public' => [
'driver' => 'local',
'root' => storage_path('app/public'),
'url' => env('BUILDER_URL') . '/storage',
'visibility' => 'public',
],
BUT... I need to somehow be able to also upload files from the VIEWER app that should end up in the same storage folder as the BUILDER.
So in my VIEWER app I would like my builder_public disk to be something like this:
'builder_public' => [
'driver' => 'local',
'root' => builder_storage_path('app/public'), // here
'url' => env('BUILDER_URL') . '/storage',
'visibility' => 'public',
],
Is there some way I can share the storage/app folder between two separate Laravel apps?
Yes, you can. if you are using Linux server, try below command
ln -s SOURCE_FOLDER DESTINATION_FOLDER
it will create folder short cut but still, your both application will use the same location(DESTINATION_PATH)
If you are using different servers, try mount command.

Laravel filesystem outside project

Can I use laravel filesystem to save file outside current project?
I have this structure: project and cdn.
I've tried custom driver but fails. It wont put any file at cdn folder.
'custom' => [
'driver' => 'local',
'root' => '~/Code/cdn.web/storage',
],
$store = Storage::disk('custom')->put('index.txt', 'contents');
I've followed this answer too Storing files outside the Laravel 5 Root Folder
But there is no custom driver.
Is there anyway to make it working?
Thanks
Found the answer.
Laravel doesn't support ~ on the path.
I need to put full path to make it working.
'custom' => [
'driver' => 'local',
'root' => '/home/vagrant/Code/cdn.web/storage',
],

Categories