Managing uploaded files in Laravel - linking public and storage directories - php

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;

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?

Issue with Storage on shared hosting

I am deploying my first project on a shared hosting.
I followed this tutorial to deploy the website and turn the public folder into the public_html folder of my hosting plan.
When I upload an image from my website (with storeAs() method), the file is uploaded in the private/storage folder, not the public one (where I would like).
The asset() function try to display the image from public_html/storage.
What can I do ?
Thank you :)
My files are like this :
private/
- app/
- bootstrap/
- config/
- database/
- resources/
- routes/
- storage/
- tests/
- vendor/
public_html/
- css/
- js/
- images/
- js/
- storage/
(Laravel 8, trying to be hosted on Hostinger)
The tutorial : https://dev.to/pushpak1300/deploying-laravel7-app-on-shared-hosting-hostinger-31cj
Why does this problem happen?
If you implement and need storage matter in your project then you need to link storage folder to the public folder for security reason and to make the URL simple, clean and readable by the users, so when you upload the laravel project to a server, you need to run the following command line to complete the linking process:
php artisan storage:link
and since you host the laravel project to a shared server, so you don’t have access to a terminal to run the above-mentioned command line . so I’ll show you a way to do that manually without using SSH or the terminal.
Steps to fix the problem
1- At localhost server-side (before uploading the project to the server), make sure that you have done filesystem configuration, then run the following command line:
php artisan storage:link
after that you will get a message “The links have been created.” and that is ok for now.
2- Upload the project to the server, the storage link will not work as you expected, so we need to start fixing this.
3- Go to the public folder you will find a folder called “storage”, delete it.
4- Go to routes folder and open “web.php” file, then copy & paste the following code at the top of the file:
Route::get('/linkstorage', function () {
Artisan::call('storage:link') // this will do the command line job
});
Don’t forget to save the changes.
Also, you can do the same process via “api.php” and using requests tools like postman.
5- Now we just need to run this code, so we need to do the GET request by entering the following URL at browser search bar:
“https://www.your-domain.com/public/index.php/linkstorage”
or
“https://www.your-domain.com/public/linkstorage”
this request will run the above code “Artisan::call('storage:link')” which is, in turn, will run the command line
PHP artisan storage: link
now you can go to the public folder and you will see the “storage” folder created again and marked as a shortcut folder and that means the public storage folder now is linked to the storage folder of the project.
6- Now if you upload or create a file to the storage folder, then it will appear in the public/storage folder too, now you can hit the URL of the file at the search bar of the browser and you will get the file successfully :).
Ok guys i found the solution !
Here is how to deploy your Laravel project on one shared hosting using public_html folder : https://dev.to/pushpak1300/deploying-laravel7-app-on-shared-hosting-hostinger-31cj
This will work but you can have a problem (like me) with the storeAs() method if you deal with file uploads.
To solve this problem, I edited the MyProject/config/filesystem.php by adding this in the available disks :
'public_folder' => [
'driver' => 'local',
'root' => 'PATH',
'url' => env('APP_URL').'/storage',
'visibility' => 'public',
]
Replace PATH with the complete path to your public storage (for example /home/you/domains/mydomain.com/public_html/storage/).
Now, open the controller which manage file upload and edit it like this :
request()->image->storeAs('/uploads', 'filename.png', 'public_folder');
This will upload the request()->image in public_html/storage/uploads/filename.png.
I hope it will help you if you had the same problem as me.

Laravel file image not show in the production server

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.

Laravel "Failed to load resource"

i have the blade in this route:
/resources/views/cms/public/views/projects/showproject.blade.php
And the code to render the picture is this:
#if (Storage::disk('projects')->has($project->slug))
<p class="lead"><img src="{{ asset('/storage/projects/'.$project->slug.'/home.png') }}" width="50%" >Home</p>
#else
<p class="lead"><img src="/assets/img/projects/{{$project->slug}}/home.png" width="50%">Home</p>
#endif
The storage disk called projects looks like here:
'projects' => [
'driver' => 'local',
'root' => storage_path('app/public/projects'),
'url' => env('APP_URL').'/storage',
'visibility' => 'public',
],
The error is this:
header.png Failed to load resource: the server responded with a status of 500 (Internal Server Error)
I think the problem is in the code on the view, i also read maybe i need use a symbolic link, isn't possible make it without?
Any help will be appreciated, if have any question ask it please.
Thanks. (I check the others question of stackoverflow before, and can't find the error).
From the Documentation https://laravel.com/docs/5.4/filesystem:
The Public Disk
The public disk is intended for files that are going to be publicly accessible. By default, the public disk uses the local driver and stores these files in storage/app/public. To make them accessible from the web, you should create a symbolic link from public/storage to storage/app/public. This convention will keep your publicly accessible files in one directory that can be easily shared across deployments when using zero down-time deployment systems like Envoyer.
To create the symbolic link, you may use the storage:link Artisan command:
php artisan storage:link
Of course, once a file has been stored and the symbolic link has been created, you can create a URL to the files using the asset helper:
echo asset('storage/file.txt');

Laravel 5.4 storage file not found

I am using laravel 5.4. I upload some document file in storage/app/public/documents folder like
$request->paper_file->store('documents');
and my file uploaded successfully.
in file system default storage is public
and public conf is like
'public' => [
'driver' => 'local',
'root' => storage_path('app/public'),
'url' => env('APP_URL').'/storage',
'visibility' => 'public',],
but when i hit /storage/documents/hQqlgifnVVH8bmrRPVdZ9aFGbhLmcc7g7bHZSX4u.pdf
it says not found 404. How to solve this issue.
By default Laravel puts files in the storage/app/public directory, which is not accessible from the outside web. So you have to create a symbolic link between that directory and your public one:
storage/app/public -> public/storage
You can do that by executing the php artisan storage:link command (docs).
This problem happen to me after moving project to different folder.
If you already have storage link generated, try to delete public/storage folder and run again:
php artisan storage:link
In laravel ^5.8:
Make sure you run the command php artisan storage:link to link storage/app/public with public/storage.
now:
// this return some like 'public/documents/file.ext'
$path = $request->paper_file->store('public/documents');
// this return some like 'storage/documents/file.ext'
$publicPath = \Storage::url( $path) );
// this return some like '< APP_URL env variable >/storage/documents/file.ext'
$url = asset( $publicPath );
When you upload an image with Laravel's storage function it is stored in a storage folder.
For example:
storage/app/public/avatars/file1.png
But when you want to access it via URL, you should access it like this:
storage/avatars/file1.png
It worked for me, using Laravel 7.x.
If you are coming from laravel 8.x and have indeed ran the command php artisan storage:link(to create a symbolic link ), but your code is not working i.e the file you are trying to load is not displaying, then you are like me.
Just awais ahmad mentioned, This solution porved helpful with my laravel 8x project. I have not added the "storage" path name from my blade file with the asset helper function so my image path was not outputing anything but when I typed the 'storage' path name like so asset('storage/uploads/pic1.png) my code worked!
Guess this might help someone.
After running php artisan storage:link it creates a symbolic link in the public folder and you can retrieve record like so <img src='{{ asset("public/$advert6") }}' class="img-responsive">. You should include the public directory

Categories