php - 如何使文件只上传图片 - Laravel
php - How to make file uploaded only image - Laravel
你好我是 laravel 的新手所以我真的需要一些帮助。我想创建一个代码,其中只有可以上传其他文件的图像不能,我尝试使用代码输入文件但是当我尝试上传 zip 文件时它仍然上传所以我真的需要帮助
这是我的table代码
<div class="col-sm-5">
{!! Form::label('photo', 'Photo:') !!}
<input type='file' name='photo' class='form-control' accept = 'image/jpeg , image/jpg, image/gif, image/png'>
这是我的控制器
public function store(CreateBannerRequest $request)
{
$input = $request->all();
//get original file name
if($request->photo == NULL)
{
Flash::error('Image must be filled');
return back();
}
$filename = Input::file('photo')->getClientOriginalName();
$input['photo'] = $filename;
$banner = $this->BannerRepository->create($input);
//upload file
Input::file('photo')->move($this->path, $filename);
Flash::success('Banner saved successfully.');
if (empty($banner)) {
Flash::error('No image available');
return redirect(route('banner.index'));
}
return redirect(route('banner.index'));
}
您可以通过验证 throuhg mimes:jpeg
和其他类型(例如 png 等)进行验证。在文档页面上查找 laravel 验证
你在前端有这样的代码:
查看
<form action="{{URL::to('upload/photo')}}" class="form-horizontal" method="POST" role="form" enctype="multipart/form-data">
<input type="file" name="photo">
<button class="btn btn-default pull-right" type="submit">Create</button>
</form>
路线
Route::post('upload/photo','TestController@uploadPhoto');
测试控制器
public function uploadPhoto(Request $request)
{
$this->validate($request, [
'photo' => 'mimes:jpeg,png,bmp,tiff |max:4096',
],
$messages = [
'required' => 'The :attribute field is required.',
'mimes' => 'Only jpeg, png, bmp,tiff are allowed.'
]
);
// Now save your file to the storage and file details at database.
}
希望你明白。
你好我是 laravel 的新手所以我真的需要一些帮助。我想创建一个代码,其中只有可以上传其他文件的图像不能,我尝试使用代码输入文件但是当我尝试上传 zip 文件时它仍然上传所以我真的需要帮助
这是我的table代码
<div class="col-sm-5">
{!! Form::label('photo', 'Photo:') !!}
<input type='file' name='photo' class='form-control' accept = 'image/jpeg , image/jpg, image/gif, image/png'>
这是我的控制器
public function store(CreateBannerRequest $request)
{
$input = $request->all();
//get original file name
if($request->photo == NULL)
{
Flash::error('Image must be filled');
return back();
}
$filename = Input::file('photo')->getClientOriginalName();
$input['photo'] = $filename;
$banner = $this->BannerRepository->create($input);
//upload file
Input::file('photo')->move($this->path, $filename);
Flash::success('Banner saved successfully.');
if (empty($banner)) {
Flash::error('No image available');
return redirect(route('banner.index'));
}
return redirect(route('banner.index'));
}
您可以通过验证 throuhg mimes:jpeg
和其他类型(例如 png 等)进行验证。在文档页面上查找 laravel 验证
你在前端有这样的代码:
查看
<form action="{{URL::to('upload/photo')}}" class="form-horizontal" method="POST" role="form" enctype="multipart/form-data">
<input type="file" name="photo">
<button class="btn btn-default pull-right" type="submit">Create</button>
</form>
路线
Route::post('upload/photo','TestController@uploadPhoto');
测试控制器
public function uploadPhoto(Request $request)
{
$this->validate($request, [
'photo' => 'mimes:jpeg,png,bmp,tiff |max:4096',
],
$messages = [
'required' => 'The :attribute field is required.',
'mimes' => 'Only jpeg, png, bmp,tiff are allowed.'
]
);
// Now save your file to the storage and file details at database.
}
希望你明白。