Php 使用干预在laravel中上载图像
我正在尝试使用干预在laravel 5.6中上传图像,它工作得很好,唯一的问题是它在数据库中存储的路径类似于Php 使用干预在laravel中上载图像,php,laravel,intervention,Php,Laravel,Intervention,我正在尝试使用干预在laravel 5.6中上传图像,它工作得很好,唯一的问题是它在数据库中存储的路径类似于/var/www/laravel/project/public/uploads/students/1534413021.jpg,我希望它将其存储为/uploads/students/1534413021.jpg。 这是我的表格 <form class="form-horizontal" method="post" action="{{ route('student.Avata
/var/www/laravel/project/public/uploads/students/1534413021.jpg
,我希望它将其存储为/uploads/students/1534413021.jpg
。
这是我的表格
<form class="form-horizontal" method="post" action="{{ route('student.Avatar.submit',$student->id)}}" enctype="multipart/form-data">
{{ csrf_field() }}
<div class="form-group ">
<label class="col-sm-2 control-label">Select Image To Upload:</label>
<div class="col-sm-10 {{ $errors->has('avatar') ? ' has-error' : '' }}">
<input type="file" name="avatar" value="{{ old('avatar') }}" class="form-control rounded">
@if ($errors->has('avatar'))
<span class="help-block">
<strong>{{ $errors->first('avatar') }}</strong>
</span>
@endif
</div>
</div>
<div class="form-group">
<div class="col-sm-4 col-sm-offset-2">
<button type="submit" class="btn btn-primary btn-rounded btn-block">Save changes</button>
</div>
</div>
</form>
and here is the controller
public function uploadAvatar(Request $request, Student $student)
{
$validatedData = $request->validate([
'avatar' => 'required | mimes:jpeg,jpg,png,bmb,gif,svg',
]);
if ($request->hasFile('avatar')) {
$image = $request->file('avatar');
$fileName = time() . '.' . $image->getClientOriginalExtension();
$location = public_path('uploads/students/'.$fileName);
Image::make($image)->resize(800,400)->save($location);
$studentUpdate = Student::where('id',$student->id)
->update([
'avatar'=>$location,
]);
if ($studentUpdate) {
return redirect()->route('students.profile',$student->id)->with('success','Student Profile Created Successfully');
}else{
return back()->withInput()->with('errors','Error adding new parent.');
}
}
return back()->withInput()->with('errors','PLease Select An Image.');
}
{{csrf_field()}}
选择要上载的图像:
@如果($errors->has('avatar'))
{{$errors->first('avatar')}
@恩迪夫
保存更改
这是控制器
公共功能上传化身(请求$Request,学生$Student)
{
$validatedData=$request->validate([
“头像”=>“必需|模拟:jpeg、jpg、png、bmb、gif、svg”,
]);
如果($request->hasFile('avatar')){
$image=$request->file('avatar');
$fileName=time().。$image->getClientOriginalExtension();
$location=public_path('uploads/students/'。$fileName);
图像::制作($Image)->调整大小(800400)->保存($location);
$studentUpdate=Student::where('id',$Student->id)
->更新([
“阿凡达”=>$location,
]);
如果($studentUpdate){
return redirect()->route('student.profile',$student->id)->带有('success','student profile Created Successfully');
}否则{
return back()->withInput()->with('errors','Error adding new parent');
}
}
return back()->withInput()->with('errors','PLease Select An Image');
}
像这样保存时更改路径:
$location = public_path('uploads/students/'.$fileName);
$location = 'uploads/students/'.$fileName;
对此:
$location = public_path('uploads/students/'.$fileName);
$location = 'uploads/students/'.$fileName;
它将自动转到public/$location
并保存文件,您在数据库中只有所需的字符串 您应该尝试以下方法:
public function uploadAvatar(Request $request, Student $student)
{
$validatedData = $request->validate([
'avatar' => 'required | mimes:jpeg,jpg,png,bmb,gif,svg',
]);
if ($request->hasFile('avatar')) {
$image = $request->file('avatar');
$fileName = time() . '.' . $image->getClientOriginalExtension();
$location = public_path('uploads/students/');
$uploadImage = Image::make($image->getRealPath())->resize(800,400);
$uploadImage->save($location.'/'.$fileName);
$imageName = public_path('uploads/students/'.$fileName);
$studentUpdate = Student::where('id',$student->id)
->update([
'avatar'=>$imageName,
]);
if ($studentUpdate) {
return redirect()->route('students.profile',$student->id)->with('success','Student Profile Created Successfully');
}else{
return back()->withInput()->with('errors','Error adding new parent.');
}
}
return back()->withInput()->with('errors','PLease Select An Image.');
}
听到这个我很高兴。如果它能帮助其他人,也能给我同样的结果,请投票。但将位置更改为$location='uploads/students/'。$fileName;解决了这个问题。