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

sir*_*ros 8 php sql database image laravel

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

好.我可以保存它,但在"存储/应用程序"文件夹而不是"公共"

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

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

仍然没有变化.

这是我的简单代码

路线:

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

调节器

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"));
}
Run Code Online (Sandbox Code Playgroud)

视图:

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

                <img src="storage/app/{{$patch}}">
Run Code Online (Sandbox Code Playgroud)

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

Ank*_*kit 25

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

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

你可以通过它访问它

Storage::disk('public')->put('filename', $file_content);
Run Code Online (Sandbox Code Playgroud)

  • 如何轻松获取 $file_content 变量?我不明白为什么使用框架存储文件如此复杂...... (2认同)

Sam*_*nch 5

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

php artisan storage:link
Run Code Online (Sandbox Code Playgroud)

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

{{ asset('storage/file.txt') }}
Run Code Online (Sandbox Code Playgroud)

或者在你的情况下:

<img src="{{ asset('storage/app/' . $patch) }}">
Run Code Online (Sandbox Code Playgroud)


Ino*_*loh 5

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

$file = request()->file('image');
$file->store('toPath', ['disk' => 'public']);
Run Code Online (Sandbox Code Playgroud)

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

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

'my_files' => [
    'driver' => 'local',
    'root'   => public_path() . '/myfiles',
],
Run Code Online (Sandbox Code Playgroud)

在控制器中:

$file = request()->file('image');
$file->store('toPath', ['disk' => 'my_files']);
Run Code Online (Sandbox Code Playgroud)

  • 与旧的“file_put_contents()”相比,这太复杂了 (3认同)