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?
Related
When I run in my local, the image file is shown. But in the production server it is not showing.
URL in my local :
http://localhost:8000/files/image/GOT7.jpg
URL in my production server :
http://myApplication.com/files/image/GOT7.jpg
.env in the local :
APP_URL=//localhost
.env in the production server :
APP_URL=https://myApplication.com
config/filesystem.php
'disks' => [
'local' => [
'driver' => 'local',
'root' => storage_path('/app'),
],
],
file location in the folder public/files/image/GOT7.jpg
How to solve it?
I'm gonna take a guess here and say that it is a symbolic link problem.
https://laravel.com/docs/8.x/filesystem#the-public-disk
You are using a filesystem named local which has its root in storage_path('/app'). This is a reference to the storage directory in your Laravel project.
This directory by default is not accessible. Nor should it ever be. Only your public folder should be accessible from the internet (so everything goes through public/index.php only).
So a solution that Laravel offers is to place a symlink in your public folder to a particular storage path. See the link above.
php artisan storage:link
This would create a symbolic link for you in public/storage to storage/app/public. This should now make your files accessible. For more finetuning see the Laravel docs link above.
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.
my following command works fine
copy($file_date['file_name_tmp_target'], $file_date['file_name_target']);
but when i do
\Storage::copy($file_date['file_name_tmp_target'], $file_date['file_name_target']);
or
\Storage::move($file_date['file_name_tmp_target'], $file_date['file_name_target']);
it give me an error following
(1/1) FileNotFoundException File not found at path:
Library/WebServer/Documents/project_name/public/report_tmp.csv
any idea? how to just copy file using Storage disk ?
You should make sure you set your disk in valid way. Storage is using config/filesystems.php and by default uses local disk which is configured like this:
'local' => [
'driver' => 'local',
'root' => storage_path('app'),
],
so the file path you pass here should be located in storage/app directory.
Actually i was doing wrong
This should be the answer, addition to Marcin Answer thanks :)
\Storage::disk('outdoor_real_time_report_path')->copy($file_date['file_name_tmp'], $file_date['file_name']);
In my Laravel 5.1 app, I'm storing images in storage/app/uploads folder.
My local disk:
<?php
'disks' => [
'local' => [
'driver' => 'local',
'root' => storage_path('app/uploads'),
],
// other configuration...
?>
Thing is, I can't figure out a way to use the image files after they're uploaded, e.g. as the source of <img> tag. Basically, I need to retrieve a valid path to an image, so it can be used on the page.
For deployment I'm using Envoyer which provides a solution. According to Envoyer docs:
When storing user uploaded files, you should store them in the storage directory of your application if you are using Laravel. Then, you may use the "Manage Linked Folders" feature of Envoyer to create a symbolic link from your public directory to the storage directory. The "Manage Linked Folders" button can be found on the "Deployment Hooks" tab of your project.
..and this is clear.
But how do I "link" the storage and public folders in my local development environment? Does Laravel provide a way to do it, or do I need to create a symbolic link in my environment manually?
There are a few options:
Create a controller that would output the files
class AssetController {
public function show($id) {
$file = File::findOrFail($id);
return Response::make(Storage::get($file->storage_key), 200, ['Content-Type' => $file->mime_type]);
}
}
Create a symlink public/assets => storage/app/
Upload files to public/assets instead of storage/app
Use rewrites on your web server to serve the files from your storage/app folder - how to do that depends on what webserver you are using. For nginx you could use something like
rewrite ^/v1/assets/(\d+) /../storage/app/$1;
I am developing an web application using Laravel 5 and AngularJS. I am using pure angularJS app for the client side and I am putting the client app files (views) in my public folder.
In Laravel 4, I could change the path from the bootstrap/start.php file. But in Laravel 5, I can not see the start.php file. So where do I change the configuration in Laravel 5?
See line 16 of config/view.php (the "View Storage Paths" section)
'paths' => [
realpath(base_path('resources/views'))
],
So you might change it to realpath(base_path('public/assets/views')) to be in your public path.
Additional Examples
'paths' => [
// src/MyNamespace/resources
realpath(base_path('src/MyNamespace/resources')),
// app/resources
realpath(app_path('resources'))
],
You can provide multiple search locations
You can use app_path(), base_path(), or neither.