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

血红的双手。 提交于 2020-12-05 11:20:11

问题


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?


回答1:


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>


来源:https://stackoverflow.com/questions/56814137/laravel-5-8-how-to-move-file-from-storage-directory-to-public-directory

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!