2016-11-28 174 views
0

我有一個表單,其中包含幾個字段,包括我想用作圖像上傳器的上傳文件按鈕(<input type="file" name="image" id="image" class='image'>)。其他領域的正常工作和上傳一切在數據庫上,然而,在圖像領域,我上傳的是給我這個文件名的文件的文件名:如何在Laravel上傳圖片文件?

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

假設你只在你的數據庫表中存儲文件名,那麼你必須在其他地方上傳圖像。所以你可以創建類似於:

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'); 
} 
+0

最好用'DIRECTORY_SEPARATOR'替換'/'。比如'$圖像 - >動(public_path()DIRECTORY_SEPARATOR '圖像' DIRECTORY_SEPARATOR '飲料' DIRECTORY_SEPARATOR,$ IMAGE_NAME。。。);' –

+0

好一點,我已經更新我的答案。謝謝開爾文。 – AWJ

+0

增值投票。解釋完整答案。 – Ronald