2016-07-06 110 views
1

我与存储::放($文件,$内容)文件只是一个简单的doubts-无法保存在laravel 5.2

我试图存储在使用Storage门面存储我的图片和视频文件。 但我很困惑,因为我无法将该文件存储在存储文件夹中。 我使用的干预形象包装,当我只是试图救我修改$文件

Storage::put($file); 它抛出错误2参数丢失等

我到处找在网上关于这个问题,我发现

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

还有一件事

我如何返回文件,它可以从不同的域访问的完整路径。 我是你使用“公”,“本地”基本上是在您将其存储在哪个磁盘建立一个API :)

感谢您的帮助提前

+1

https://laravel.com/docs/5.2/filesystem'放(文件名,目录)' –

+0

在文档Mike B链接(搜索头像)中有一个保存二进制文件的例子。 – herrjeh42

+0

我已经做过,但仍然困惑的是什么$内容是在Storage :: put('file.jpg',$ content); :( – Rahul

回答

0

\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); 
+0

哇,谢谢soooo多西蒙,它只是像魅力工作..非常感谢您的时间。:) – Rahul

+0

不是问题,很高兴帮助 –

2

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')); 

在问候你如何来指代路径作为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" 
+0

我也试过这个,但是我得到一个错误''image-> encode()'''错误 - '''调用成员函数编码()字符串'''我已经得到了我想要的西蒙回答这将是一个伟大的奖金:) – Rahul

+0

@Rahul我假设你的实例干预图像是'$图像';在重新阅读你的问题后,你的干预图像实例是'$ file'吗?如果是这样,用'$ file'替换'$ image' –

+0

克里斯,现在它工作得很好,非常感谢你的帮助,感谢你的时间:) – Rahul