Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel 5.8 - How to move file from storage directory to public directory?

I would like to move one of my files from the storage file directory in Laravel to the public file directory in laravel, without changing my file system configuration. Is this possible?

like image 952
Aiden Kaiser Avatar asked Jun 28 '19 23:06

Aiden Kaiser


1 Answers

Yes, you can do it with Storage::move and public_path() helper.

Storage::move( 'old/file.jpg', public_path('new/file.jpg') );

Note that old/file.jpg is relative to your-project/storage/app, so the file that you want to move would be in your-project/storage/app/old/file.jpg


UPDATE

Still, another approach could be to move/store the file in laravel-project/storage/app/public.

And then have it accessible in the public directory via a symbolic link.

After that, all the files and directories inside laravel-project/storage/app/public will be linked to laravel-project/public/storage/ directory. Then you can access the file by url.

So, to do it in this way, move the file to storage/app/public/ folder:

Storage::move( 'old/file.jpg', 'public/movedfiles/file.jpg' );

Create the symbolic link by this artisan command:

php artisan storage:link

Then you can access the file by url:

<a href='http://your-domain/storage/movedfiles/file.jpg'>file.jpg</a>

or in blade

<a href='{{ asset('storage/movedfiles/file.jpg') }}'>file.jpg</a>
like image 83
porloscerros Ψ Avatar answered Nov 02 '22 18:11

porloscerros Ψ