【问题标题】:How to store image path of storage\app\public in laravel-5.4如何在 laravel-5.4 中存储 storage\app\public 的图片路径
【发布时间】:2017-09-12 09:06:19
【问题描述】:

我正在使用 laravel-5.4 make:auth。在register.blade.php 中,为用户添加了一个额外的字段个人资料图片

<form class="form-horizontal" role="form" method="POST" action="{{ route('register') }}" enctype="multipart/form-data">
    {{ csrf_field() }}
    <div class="form-group{{ $errors->has('image') ? ' has-error' : '' }}">
        <label for="image" class="col-md-4 control-label"> Profile picture</label>
        <div class="col-md-6">
            <input id="image" type="file" class="form-control" name="image">

            @if ($errors->has('image'))
                <span class="help-block">
                    <strong>{{ $errors->first('image') }}</strong>
                </span>
            @endif
        </div>
    </div>

我想将图像路径存储在数据库中。我也执行了: php artisan storage:link[public/storage] 目录已链接。

app\Http\Controllers\Auth\RegisterController.php:

public function store(Request $request)
{
    if($request->hasFile('image')) {
        $image_name = $request->file('image')->getClientOriginalName();              
        $image_path = $request->file('image')->store('public'); 
        $image = Image::make(Storage::get($image_path))->resize(320,240)->encode();
        Storage::put($image_path,$image);
        $image_path = explode('/',$image_path);
        $user->image = $image_path;
        $user->save();
    } else{
        return "No file selected";
    }

}

web.php

Route::post('/store', 'RegisterController@store');
Route::get('/show', 'RegisterController@show');

在数据库中,图像下的用户表中存储为临时路径:

C:\xampp\tmp\phpC762.tmp.

如何存储storage\app\public的图片路径。

【问题讨论】:

    标签: database image laravel file-upload laravel-5.4


    【解决方案1】:

    在您的控制器中更改此代码

    $user->image = $image_path;
    

    $user->image = Storage::url($image_name);
    

    文档:https://laravel.com/docs/5.4/filesystem#file-urls

    【讨论】:

    • 它没有帮助。我在 registercontroller.php 中的存储功能甚至没有执行。
    • 有什么错误吗?当你dd($image_path); 时你会得到什么?
    • 它甚至没有进入 store 函数。我已经使用了现有的 registercontroller[app\Http\Controllers\Auth\RegisterController.php] 并在那里定义了 store 函数。当用户在表单[register.blade.php]中注册时,调用的动作是-> action="{{ route('register') }}"。用户点击提交后,进入registercontroller验证用户信息。我的问题是,为什么控制器中没有执行存储功能。我是否以正确的方式定义。
    • 有命名路由register 吗?
    • 是的。在 \vendor\laravel\framework\src\Illuminate\Routing\Router.php 下: $this->get('register', 'Auth\RegisterController@showRegistrationForm')->name('register'); $this->post('register', 'Auth\RegisterController@register');
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-10-23
    • 2016-08-19
    • 2016-10-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-09
    相关资源
    最近更新 更多