I use Laravel Storage::putFile() when I store uploaded files and I like the convenience of it's automatic unique file name and how it takes care of file extension.

Now I want to get a file from a remote server (file_get_contents($url)) and store it like I did for uploaded files, but I don't find any equal method in the docs.

In https://laravel.com/docs/5.5/filesystem#storing-files in the put method, you have to specify the file name.

推荐答案

$filename = uniqid().File::extension($file->getClientOriginalName());
//uniqid() is php function to generate uniqid but you can use time() etc.

$path = "Files/Images/"
Storage::disk('local')->put($path.$filename,file_get_contents($file));

Laravel相关问答推荐

Inertiajs - 使用链接组件添加记录但无法在不使用表单的情况下清除文本区域

"public/index.php" 可以在 laravel 中按组写入

语法错误或访问冲突:1115 未知字符集:utf8mb4

FileViewFinder.php 第 137 行中的 Laravel 5 InvalidArgumentException:未找到视图 [.admin]

Laravel Blade:@stop VS @show VS @endsection VS @append

Laravel - 加载常见的页眉和页脚以查看

如何对有序的has many关系进行分页?

Laravel 控制器中一种特定方法的中间件

Laravel Blade 模板 @foreach 订单

Lumen和 MongoDB?

如何使用 ajax 请求删除 laravel 5.3 中的记录?

Laravel - 批量分配异常错误

Laravel - 更新时禁用更新时间

Laravel Blade - 如果满足条件,则添加一个类

在 Laravel 应用程序中在哪里指定应用程序版本?

无法捕获 Carbon 抛出的异常

将 Laravel 集合附加到另一个集合

在 laravel 中安装 vue 3.0

如何在 vs 代码中自动导入 laravel 类

count() 参数必须是数组或在 laravel 中实现可数的对象