无法在 Laravel 5.2 中使用 Storage::put($file, $content) 保存我的文件

Rah*_*hul 5 php file laravel laravel-5.2

只是一个简单的疑问——

我正在尝试使用存储外观将我的图像和视频文件存储在存储中。但我很困惑,因为我无法将该文件存储在存储文件夹中。我正在使用干预图像包,当我尝试保存修改后的 $file 时

Storage::put($file); 它抛出错误,缺少 2 个参数等。

我在网上到处搜索这个问题,我发现

Storage::put($file , $content) 现在当我尝试将图像保存到我的文件夹时,在这种情况下 $content 是什么?请举个例子-

还有一件事情

我如何返回可以从不同域访问的该文件的完整路径。我正在构建一个 API :)

我在这里先向您的帮助表示感谢

Sim*_*ies 5

您使用“公共”、“本地”的什么磁盘基本上存储它?

   \Storage::disk('local')->put($file_name, $the_file);
Run Code Online (Sandbox Code Playgroud)

可以在config/filesystems.php文件中查看磁盘设置。

$content也是您要保存的实际项目/文件。这是我最近所做的一个例子..

   // the $image is from $request->file('file');
   $thefile = \File::get($image);
   $file_name = $title->slug . '-' . $year . '.jpg';
    \Storage::disk('local')->put($file_name, $thefile);
Run Code Online (Sandbox Code Playgroud)


Chr*_*nce 5

in 的第二个参数是Storage::put($filename, $content)指构成文件的原始信息。对于文本文件,$content将是文本,而对于图像,它将是构成图像的原始信息。

由于您使用的是 Intervention Image,因此您可以使用该encode()方法来获取存储所需的数据。

// Assuming that $file is your Intervention Image instance
// Example 1: Save as the default type (JPEG)
Storage::put('public/images/my_profile.jpeg', $file->encode());

// Example 2: Save as a PNG
Storage::put('public/images/my_profile.png', $file->encode('png'));
Run Code Online (Sandbox Code Playgroud)

关于如何将该路径作为 URL 引用的问题,请确保遵循文档中的以下代码段:

注意:使用本地驱动时,一定要在 public/storage 创建一个符号链接,指向 storage/app/public 目录。

之后,您将能够storage/app/public从浏览器访问所有内容,如下所示:

echo config('app.url') . str_replace('/public/', '/', Storage::url('public/images/my_profile.png'));
// Echoes "http://localhost/storage/images/my_profile.png"
Run Code Online (Sandbox Code Playgroud)