php 将图像保存在公共文件夹中而不是存储 laravel 5

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

save image in public folder instead storage laravel 5

phpsqldatabaseimagelaravel

提问by siros

i wanna save my avatar at "Public" folder and ther retrieve.

我想将我的头像保存在“公共”文件夹中,然后检索。

ok. i can save it but in "storage/app" folder instead "public"

好的。我可以将它保存在“storage/app”文件夹而不是“public”

my friend told me go to "config/filesystem.php" and edit it ,so i did it like this

我的朋友告诉我去“config/filesystem.php”并编辑它,所以我是这样做的

 'disks' => [
   'public' => [
        'driver' => 'local',
        'root' => storage_path('image'),
        'url' => env('APP_URL').'/public',
        'visibility' => 'public',
    ],

still no change.

仍然没有变化。

here my simple codes

这是我的简单代码

Route :

路线 :

Route::get('pic',function (){
return view('pic.pic');
});
Route::post('saved','test2Controller@save');

Controller

控制器

public function save(Request $request)
{
        $file = $request->file('image');
        //save format
        $format = $request->image->extension();
        //save full adress of image
        $patch = $request->image->store('images');

        $name = $file->getClientOriginalName();

        //save on table
        DB::table('pictbl')->insert([
            'orginal_name'=>$name,
            'format'=>$base,
            'patch'=>$patch
        ]);

        return response()
               ->view('pic.pic',compact("patch"));
}

View:

看法:

{!! Form::open(['url'=>'saved','method'=>'post','files'=>true]) !!}
                {!! Form::file('image') !!}
                {!! Form::submit('save') !!}
            {!! Form::close() !!}

                <img src="storage/app/{{$patch}}">

How Can save my image (and file in future) at public folder instead storage?

如何将我的图像(和将来的文件)保存在公共文件夹而不是存储中?

回答by Ankit24007

In config/filesystems.php, you could do this... change the root element in public

在 config/filesystems.php 中,您可以这样做...更改公共中的根元素

'disks' => [
   'public' => [
       'driver' => 'local',
       'root'   => public_path() . '/uploads',
       'url' => env('APP_URL').'/public',
       'visibility' => 'public',
    ]
]

and you can access it by

你可以通过

Storage::disk('public')->put('filename', $file_content);

回答by Inoyatulloh

You can pass disk option to method of \Illuminate\Http\UploadedFileclass:

您可以将磁盘选项传递给\Illuminate\Http\UploadedFile类方法:

$file = request()->file('image');
$file->store('toPath', ['disk' => 'public']);

or you can create new Filesystem disk and you can save it to that disk.

或者您可以创建新的文件系统磁盘并将其保存到该磁盘。

You can create a new storage disc in config/filesystems.php:

您可以在以下位置创建新的存储光盘config/filesystems.php

'my_files' => [
    'driver' => 'local',
    'root'   => public_path() . '/myfiles',
],

in controller:

在控制器中:

$file = request()->file('image');
$file->store('toPath', ['disk' => 'my_files']);

回答by Samsquanch

You'll need to link your storage directory to your public folderwith:

您需要使用以下命令将存储目录链接到公用文件夹

php artisan storage:link

Once you've done that, to display it in the view you can do:

完成后,要在视图中显示它,您可以执行以下操作:

{{ asset('storage/file.txt') }}

Or in your case:

或者在你的情况下:

<img src="{{ asset('storage/app/' . $patch) }}">