我下面有功能代码,但它不会将照片保存在数据库或文件路径中。
public function store(Request $request)
{
$this->validate($request, array(
'name' => 'required',
'price' => 'required|numeric',
'type_id' => 'required|numeric',
'photo' => 'required',
));
$item = new Menu;
$item->name = $request->input('name');
$item->price = $request->input('price');
$item->type_id = $request->input('type_id');
if ($request->hasFile('photo')) {
$photo = $request->file('photo');
$filename = 'MenuItem' . '-' . time() . '.' . $photo->getClientOriginalExtension();
$location = public_path('images/'. $filename);
Image::make($photo)->resize(500, 500)->save($location);
$item->photo = $filename;
}
$item->save();
Session::flash('success', 'Menu Item Saved Successfully.');
return redirect()->back();
}
dd($request->all());
以上的代码array:5 [▼
"_token" => "awAvc7F8lOv9vKkfwyiTFj7jnQGszv8xjLQxcwRH"
"name" => "test"
"price" => "100"
"photo" => "air putih.jpg"
"type_id" => "1"
]
有什么主意吗?
基于下面的Nabil Farhan
答案,我忘记了enctype="multipart/form-data"
,但现在我明白了
Intervention \ Image \ Exception \ NotReadableException
Unable to find file ().
仍然无法保存我的照片。
我再次添加请求,现在添加enctype="multipart/form-data"
后变得很奇怪:
array:5 [▼
"_token" => "awAvc7F8lOv9vKkfwyiTFj7jnQGszv8xjLQxcwRH"
"name" => "kerupuk"
"price" => "2000"
"type_id" => "3"
"photo" => UploadedFile {#805 ▼
-test: false
-originalName: "kerupuk.jpg"
-mimeType: "image/jpeg"
-error: 0
#hashName: null
path: "C:\Windows\Temp"
filename: "phpB195.tmp"
basename: "phpB195.tmp"
pathname: "C:\Windows\Temp\phpB195.tmp"
extension: "tmp"
realPath: false
aTime: 2019-02-12 04:57:39
mTime: 2019-02-12 04:57:39
cTime: 2019-02-12 04:57:39
inode: 0
size: 43933
perms: 0100666
owner: 0
group: 0
type: "file"
writable: true
readable: true
executable: false
file: true
dir: false
link: false
linkTarget: "C:\Windows\Temp\phpB195.tmp"
}
]
为什么我的照片场变成那样?!
无论如何,这是我在刀片中的表格:
{{ Form::open(array('route' => 'menus.store', 'files' => true)) }}
<div class="row">
<div class="col-md-12">
<h5>Name</h5>
{{ Form::text('name', null, array('class' => 'form-control')) }}
</div>
<div class="col-md-12">
<h5>Price</h5>
{{ Form::number('price', null, array('class' => 'form-control')) }}
</div>
<div class="col-md-12">
<h5>Photo</h5>
{{ Form::file('photo', array('class' => 'form-control', 'id' => 'photo')) }}
</div>
<div class="col-md-12">
<h5>Type</h5>
<select name="type_id" id="type_id" class="form-control">
<option value="">-- Select --</option>
@foreach($types as $type)
<option value="{{$type->id}}">{{$type->name}}</option>
@endforeach
</select>
</div>
<div class="col-md-12 mt-2">
{{ Form::submit('Save', array('class' => 'btn btn-primary')) }}
</div>
</div>
{{ Form::close() }}
答案 0 :(得分:1)
"photo" => "air putih.jpg"
不应为字符串。它应该具有有关文件的更多信息。
我认为问题出在刀片文件中。请检查您是否在表单标签中使用了enctype='multipart/form-data'
。
编辑
更改此
Image::make($photo)->resize(500, 500)->save($location);
对此
Image::make($photo->getRealPath())->resize(500, 500)->save($location);
答案 1 :(得分:0)
尝试更改此行
Image::make($photo)->resize(500, 500)->save($location);
Image::make($photo->getRealPath())->resize('200','200')->save($location);
您的真实路径是错误的检查配置
答案 2 :(得分:0)
您应该尝试以下操作:
您的查看文件是这样的:
{!! Form::open(['route' => 'menus.store', 'class' => 'form-horizontal', 'role' => 'form', 'method' => 'post','files'=>true]) !!}
<div class="row">
<div class="col-md-12">
<h5>Name</h5>
{{ Form::text('name', null, array('class' => 'form-control')) }}
</div>
<div class="col-md-12">
<h5>Price</h5>
{{ Form::number('price', null, array('class' => 'form-control')) }}
</div>
<div class="col-md-12">
<h5>Photo</h5>
{{ Form::file('photo', array('class' => 'form-control', 'id' => 'photo')) }}
</div>
<div class="col-md-12">
<h5>Type</h5>
<select name="type_id" id="type_id" class="form-control">
<option value="">-- Select --</option>
@foreach($types as $type)
<option value="{{$type->id}}">{{$type->name}}</option>
@endforeach
</select>
</div>
<div class="col-md-12 mt-2">
{{ Form::submit('Save', array('class' => 'btn btn-primary')) }}
</div>
</div>
{{ Form::close() }}
您的控制器功能如下:
use Input;
public function store(Request $request)
{
$this->validate($request, array(
'name' => 'required',
'price' => 'required|numeric',
'type_id' => 'required|numeric',
'photo' => 'required',
));
$item = new Menu;
$item->name = $request->input('name');
$item->price = $request->input('price');
$item->type_id = $request->input('type_id');
if ($request->hasFile('photo')) {
$photo = Input::file('photo');
$filename = 'MenuItem' . '-' . time() . '.' . $photo->getClientOriginalExtension();
$location = public_path('images/'. $filename);
$img = Image::make($photo->getRealPath());
$img->resize(500, 500, function ($constraint) {
$constraint->aspectRatio();
})->save($location);
$item->photo = $filename;
}
$item->save();
Session::flash('success', 'Menu Item Saved Successfully.');
return redirect()->back();
}