Using storage_path from another Laravel app - php

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.

Related

Change default "/public" to "public_html" in production

I'm putting my Laravel project in production and I've clone it from GitHub to /home/myuser/repositories/myuser/MYPROJECT-app, and my public Laravel folder content is in /home/myuser/public_html.
I've changed (project location)/app/Providers/AppServiceProvider.php and add:
$this->app->bind('path.public', function() {
return '/home/myuser/public_html';
});
To the Register function. I changed the index.php from the public folder to match my project location and everything works well, except when I try to upload a file using the method:
Storage::disk('public')->put('myfiles/', $request->myFile)
It is stored in /home/myuser/repositories/MYPROJECT-app/public/storage/myfiles instead of /home/myuser/public_html/storage/myfiles.
(Note: I cannot use symbolic links because some restrictions with the server configuration, so I'm trying to store all the files within a storage folder within the public path).
I'm guessing I'm missing some configuration to tell Laravel to store the uploaded files in /public_html/storage instead of MYPROJECT/public/storage, but I can't find which file I have to change.
This should work for you, but I cannot assure it 100%. (I will be using Laravel 9.x).
In your config/filesystems.php, go to the disks index and add a new one just for testing purposes:
'outside' => [
'driver' => 'local',
'root' => realpath('/home/myuser/public_html'),
'url' => env('APP_URL').'/storage',
'visibility' => 'public',
'throw' => false,
],
As you can see, we are using realpath('/home/myuser/public_html'), but have in mind that url requires a symlink and you said it is not possible, so you are basically done there.
Let me know if this partially works, does it works but the URL not? Can you read files, store them, etc?

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.

Laravel Nova uploaded image file not showing on staging environment

I have deployed a laravel application which is using the Nova admin for backend management on Bluehost VPS account. I have creates a symlink between storage and public as recommend in the laravel documentation.
I have set up a resource for uploading images like so
Avatar::make('Image')->onlyOnIndex(),
Image::make('Image')->disk('public')
->path('item-category')->required()->hideFromIndex()
I am using the public disk for storage with the filesystem disk configuration like so
'public' => [
'driver' => 'local',
'root' => storage_path('app/public'),
'url' => env('APP_URL').'/storage',
'visibility' => 'public',
],
On my local system, I am able to see the actual image displayed but on the staging the image is uploaded successfully but does not display because the image is not found
The record is stored in the db like so
Why does this same configuration work on my local dev system but not on the staging server? Spent the best part of my morning trying to figure this out.
Try running the artisan command php artisan storage:link, that will create a symlink from your public folder to the storage folder.

Accessing public directory laravel 5.5 deployed on shared hosting

I deployed a Laravel 5.5 application on shared hosting and I have a problem with uploading/viewing images.
Everything is working well on localhost but when I upload it online I can't access the directory where my images are stored.
Here is where is store my images
$destinationPath = public_path('items/'.$request->get('modal-item-id'));
And it is storing image in :
public_html/nbd/public/items/307/my-image.png
Also here is my filesystems.php settings for 'images':
'images' => [
'driver' => 'local',
'root' => public_path('items'),
'url' => env('APP_URL').'/items',
visibility' => 'public',
],
My app is located in
public_html/nbd/system
...and because I had to put PUBLIC folder out of the systems folder (shared hosting) now I can't access the image on https://my-url/system/public/items/307/my-image.png
How can I access that file? Where did I make a mistake?
You should not run the full Laravel under a webserver document root which is in your case ./public_html more point the documentroot of the webserver to ./public_html/nbd/system/public -> and you will be able to access http://your-domain/items/307/my-image.png
But in your case you cannot access due missing folder nbd -> https://my-url/nbd/system/public/items/307/my-image.png

Laravel error: symlink() has been disabled for security reasons

I'm trying to set-up a laravel cms system that already exists on a shared host. But when i setup my laravel project on this shared hosting directory i get the error
symlink() has been disabled for security reasons
I can't seem to find a proper explenation on how i can fix this in my situation. I only get this error the first time and when i refresh this error, it disappears.
in your project's public directory, manually create a storage folder.
this worked for me
You can create symlink for your desired directory by logging in to your server via SSH download Putty and then login to your server via putty by using your server credentials. You can use linux commands to create a symbolic link:
ln -s TARGET LINK_NAME
Where the -s makes it symbolic.
There are 2 options to solve this;
Use any of the supported services for file storage i.e Amazon S3, Dropbox, FTP, Rackspace
Follow the steps below;
Create a folder in /public folder named /storage
Move all folders from /storage/app/public/ to the folder you created /public/storage/
Open file config/filesystems.php and change the values as shown;
'local' => [
'driver' => 'local',
'root' => public_path('storage'),
],
'public' => [
'driver' => 'local',
'root' => public_path('storage'),
'url' => env('APP_URL') . '/storage',
'visibility' => 'public',
],
Now open file .env and change or create the values as shown below;
AVATAR_DIR=avatars
SIGNATURE_DIR=signatures
LOGOS_DIR=logos
MEDIA_DIR=media
After a lot struggle this trick work for me at shared hosting server for Laravel project.
Connect with terminal at server via SSH or directly from CPanel
Run this command at your terminal
ln -s /folder-path-with-name-to-be-linked /folder-paht-where-you-want-to-link-it
e.g in my case
ln -s /home/user-name/public_html/domain-folder-name/storage/app/public /home/user-name/public_html/domain-folder/public/
Note: You don't need to change in php.ini and .htaceess files.
Reference Link:
https://unix.stackexchange.com/questions/141436/too-many-levels-of-symbolic-links/141442#141442?newreg=14b52f3d6fcb454399a1a1a66e2170df
The problem could be that your hosting provider will not let you create the smlink.Have you checked if this is the case?
One suggestion if so, is to write the files that you would have put in the symlink folder directly into the public area of the site so that a symlink is not required.

Categories