如何在Laravel上传图像文件?

时间:2016-11-28 01:55:37

标签: html css laravel

我有一个包含多个字段的表单,包括我想用作图片上传器的上传文件按钮(<input type="file" name="image" id="image" class='image'>)。其他字段工作正常并上传数据库中的所有内容,但是,在图像字段中,我上传的文件的文件名给了我这个文件名:

  

C:\ laragon \ TMP \ php859A.tmp

......我该怎么办?谢谢:)以下是代码:

这是表格:

<html>
<div class="col-md-offset-4 col-md-4">
        <form action="{{ $action }}" method="POST" enctype="multipart/form-data">
        <h1> {{ $header }} </h1>
            <div class="form-group">
                <label for="exampleInputEmail1">Name</label>
                <input type="text" class="form-control" id="name" name="name" placeholder="Name" value="{{ $name }}">
            </div>
            <div class="form-group">
                <label for="exampleInputPassword1">Description</label>
                <input type="text" class="form-control" id="description" name="description" placeholder="Description" value="{{ $description }}">
            </div>
            <div class="form-group">
                <label for="exampleInputPassword1">Price</label>
                <input type="number" class="form-control" id="product_price"  name="product_price" placeholder="Price" value="{{ $product_price }}">                    
            </div>

            <div class="form-group">
                <label for="image">SELECT IMAGE TO UPLOAD</label><br>
                <input type="file"  name="image" id="image" class='image'>
            </div>

            <div class="col-md-offset-2">
            {!! csrf_field() !!}
            <button type="submit" class="btn btn-default col-md-5" style="background:#0099ff; color:#f2f2f2;">{{ $button }}</button>
            <button type="submit" class="btn btn-default col-md-5" style="background:#f4f4f4; color:#000;"><a href="{{ route('beverages') }}">Cancel</a></button>
            </div>
        </form>
    </div>
</html>

这些是我的功能在控制器中创建和存储:

public function create()
{
  $data['action']           = route('beverage_store');  
  $data['button']           = 'Add';
  $data['header']           = 'ADD BEVERAGE';
  $data['name']             = old('name');  
  $data['description']      = old('description');    
  $data['product_price']    = old('product_price');
  $data['image']            = old('image');
  return view('layouts.beverages.beverageform',$data);
}

/**
 * Store a newly created resource in storage.
 *
 * @param  \Illuminate\Http\Request  $request
 * @return \Illuminate\Http\Response
 */
public function store(Request $request)
{

    $input = $request->all();
    #dd($input);
    BeveragesModel::create($input);
    return redirect()->route('beverages');
}

:)

1 个答案:

答案 0 :(得分:1)

假设您只将文件名存储在数据库表中,那么您必须将图像上传到其他位置。所以你可以创建类似的东西:

private function upload($request)
{
    $image_name = '';
    if($request->hasFile('image'))
    {
        $image = $request->file('image');
        $image_name = md5(uniqid()) . '.' . $image->getClientOriginalExtension();
        $image->move(public_path() . DIRECTORY_SEPARATOR . 'images' . DIRECTORY_SEPARATOR . 'beverages' . DIRECTORY_SEPARATOR, $image_name);
    }
    return $image_name;
}

public function store(Request $request)
{
    $image_name = $this->upload($request);
    $input = $request->all();
    $beverage = BeveragesModel::create($input);
    $beverage->image = $image_name;
    $beverage->save();
    return redirect()->route('beverages');
}