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

时间:2016-07-06 20:59:30

标签: php file laravel laravel-5.2

只是一个简单的疑惑 -

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

token 它失去了2个参数丢失等错误。

我在网上到处搜索这个问题,我找到了

Storage::put($file); 现在,在我尝试将图像保存到我的文件夹时,$ content的内容是什么?请举个例子 -

还有一件事

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

感谢您的帮助

2 个答案:

答案 0 :(得分:3)

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

由于您正在使用干预图像,因此您可以使用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'));

关于如何将该路径引用为网址的问题,请务必遵循文档中的以下代码段:

  

注意:使用本地驱动程序时,请务必在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"

答案 1 :(得分:1)

您使用'公共','本地'哪个磁盘基本上存放在哪里?

   \Storage::disk('local')->put($file_name, $the_file);

可以在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);