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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/38233688/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 14:06:32  来源:igfitidea点击:

Unable to save my files with Storage::put($file, $content) in laravel 5.2

phpfilelaravellaravel-5.2

提问by Rahul

Just a simple doubts-

只是一个简单的疑问——

i'm trying to store my image and video file on storage using Storage facade. but i'm confused because i'm unable to store that file on storage folder. i'm using intervention image package and when i just try to save my modified $file

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

Storage::put($file);it's throwing error that 2 argument missing etc.

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

i searched everywhere on the net regarding this issue and i found

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

Storage::put($file , $content)now what is $content in this case when i'm trying just save an image to my folder ? please give me example-

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

and one more thing

还有一件事情

How do i return a full path of that file which can be accessible from different domain. i'm building an API :)

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

Thanks for the help in advance

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

回答by Chris Forrence

The second parameter in Storage::put($filename, $content)refers to the raw information that makes up the file. For a text file, $contentwould be the text, while for an image, it'd be the raw information that makes up the image.

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

Since you're using Intervention Image, you can use the encode()method to get the data needed for storage.

由于您使用的是 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'));

In regards to your question about how to refer to that path as a URL, make sure to follow the following snippet from the documentation:

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

Note: When using the local driver, be sure to create a symbolic link at public/storage which points to the storage/app/public directory.

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

After that, you'll be able to access everything from storage/app/publicfrom the browser like such:

之后,您将能够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"

回答by Simon Davies

What disk your using 'public', 'local' basically where your storing it?

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

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

The disk settings can be view in the config/filesystems.phpfile.

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

Also the $contentis the actually item/file you want to save. Here is an example of what ive done recently..

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