使用类 Storage Laravel 5.2 上传文件/图像

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/37029383/
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 13:43:25  来源:igfitidea点击:

Upload File/Image with class Storage Laravel 5.2

phpimagelaravellaravel-5

提问by Darma Kurniawan Harefa

First, I'm sorry for my bad English.

首先,我很抱歉我的英语不好。

I want to upload a file/image from my driver to my project directory using class Storage. I want that every file/image will be uploaded/moved to my public/imgdirectory. I use Form::file('img')on my views and on my post controller, I write this

我想使用 class 将文件/图像从我的驱动程序上传到我的项目目录Storage。我希望每个文件/图像都将上传/移动到我的public/img目录中。我Form::file('img')在我的观点和我的帖子控制器上使用,我写这个

 $img = Input::file('img');
    if ($img !== null) {
        $filename       = $img->getClientOriginalName();
        Storage::disk('uploads')->put('filename', $filename);

        $jenis->img     = $filename;  
    }

and on my config/filesystemI write this

在我的config/filesystem我写这个

'uploads' => [
        'driver' => 'local',
        'root'   => public_path() . '/img',
    ],

But, nothing happen on my public/imgdirectory, no new file/image on there. Can u help me whats wrong with my code? and I hope u guys can help me with another good way on how to upload a file/image in laravel

但是,我的public/img目录没有任何反应,那里没有新文件/图像。你能帮我看看我的代码有什么问题吗?我希望你们能用另一种好方法帮助我如何在 laravel 中上传文件/图像

回答by Matt McDonald

Looks like your problem is you're not storing the file, you're referencing its name not its contents.

看起来你的问题是你没有存储文件,你引用的是它的名字而不是它的内容。

Try this:

尝试这个:

Storage::disk('uploads') -> put($filename, file_get_contents($img -> getRealPath()));

回答by Sachin Aghera

    if ($request->hasFile('original_pic')) {
            $original_pic = $request->file('original_pic');

            $file_extension=$original_pic>getClientOriginalExtension();
            $filename = time() . '.' . $file_extension;

            # upload original image
            Storage::put('ArticlesImages/' . $filename, (string) file_get_contents($original_pic), 'public');

            # croped image from request.
            $image_parts = explode(";base64,", $request->input('article_image'));
            $image_base64 = base64_decode($image_parts[1]);

            Storage::put('ArticlesImages/croped/' . $filename, (string) $image_base64, 'public');

            # get image from s3 or local storage.
            $image_get = Storage::get('ArticlesImages/croped/' . $filename);

            # resize 50 by 50 1x
            $image_50_50 = Image::make($image_get)
                    ->resize(340, 227)
                    ->encode($file_extension, 80);

            Storage::put('ArticlesImages/1x/' . $filename, (string) $image_50_50, 'public');

            $file_url = Storage::url('ArticlesImages/croped/' . $filename);

            return response()->json(['success' => true, 'filename' => $filename, 'file_url' => $file_url], 200);
        } 

回答by Juan Manuel Barea Martínez

In my filesystem file I configure my image directory in this way:

在我的文件系统文件中,我以这种方式配置我的图像目录:

'uploads' => [
    'driver' => 'local',
    'root'   => public_path("/img"),
],

I think that you can use your way but is another point.

我认为您可以使用自己的方式,但这是另一点。

To get the file from your view you should use File::get Laravel function:

要从您的视图中获取文件,您应该使用 File::get Laravel 函数:

$filename = $img->getClientOriginalName();
Storage::disk('uploads')->put($filename, \File::get($file));

With this would be enough, you save the file with the name of file uploaded in directory specify in filesystem.

这样就足够了,您可以使用文件系统中指定的目录中上传的文件名保存文件。