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

时间:2021-03-23 00:22:42

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

我想将我的头像保存在“公共”文件夹中并进行检索。

ok. i can save it but in "storage/app" folder instead "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?

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

2 个解决方案

#1


8  

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

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

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

#2


1  

You'll need to link your storage directory to your public folder with:

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

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) }}">

#1


8  

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

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

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

#2


1  

You'll need to link your storage directory to your public folder with:

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

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) }}">