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');
}
:)
最好用'DIRECTORY_SEPARATOR'替換'/'。比如'$圖像 - >動(public_path()DIRECTORY_SEPARATOR '圖像' DIRECTORY_SEPARATOR '飲料' DIRECTORY_SEPARATOR,$ IMAGE_NAME。。。);' –
好一點,我已經更新我的答案。謝謝開爾文。 – AWJ
增值投票。解釋完整答案。 – Ronald