Reputation: 664
I have a form for adding new profile. In the form, there is a file input field to enable user upload his/her picture to be saved in DB as its pathname to image saved in folder server.
However, my testing shows that it doesn't detected input field has image and doesn't go into if ($request->hasFile('image')) statement loop. This is the form field for file upload.
<div class="form-group">
<div class="row">
<label for="gambar" class="col-md-3 control-label">Gambar (Saiz gambar 250x300 pixels)</label>
<div class="col-md-7">
{!! Form::file('image', array('class' => 'form-control')) !!}
</div>
</div>
</div>
This is the controller function to upload the image:
// **************************
if ($request->hasFile('image')) {
$image = $request->input('image');
$photo = $request->file('image')->getClientOriginalExtension();
$destination = public_path() . '/images/';
$request->file('image')->move($destination, $photo);
$data = ['name' => $request->nama,
'No' => $request->no_id,
'nokp' => $request->no_kp,
'dd' => $dd,
'yy' => $request->yy,
'SectionID' => $request->sections,
'CategoryID' => $request->kategori,
'OperasiID' => $request->pangkat,
'AgamaID' => $request->agama,
'JantinaID' => $request->jantina,
'BangsaID' => $request->bangsa,
'nolahir' => $request->no_surat_lahir,
'kumdarah' => $request->kumdarah,
'Picture' => $request->$photo,
];
$itemregistrations = DB::table('itemregistrations')->insert($data);
if($itemregistrations)
return redirect('profil');
else
return redirect()->back()->withInput();
} else {
$data = ['name' => $request->nama,
'No' => $request->no_id,
'nokp' => $request->no_kp,
'dd' => $dd,
'yy' => $requestyy,
'SectionID' => $request->sections,
'CategoryID' => $request->kategori,
'OperasiID' => $request->pangkat,
'AgamaID' => $request->agama,
'JantinaID' => $request->jantina,
'BangsaID' => $request->bangsa,
'nolahir' => $request->no_surat_lahir,
'kumdarah' => $request->kumdarah,
// 'Picture' => $request->$filePath,
];
$itemregistrations = DB::table('itemregistrations')->insert($data);
if($itemregistrations)
return redirect('profil');
else
return redirect()->back()->withInput();
}
Upvotes: 1
Views: 403
Reputation: 406
Make sure your form have following attribute
enctype="multipart/form-data"
and use following laravel file upload code
if ($request->hasFile('image')) {
$is_file = true;
$file_name = $input['image'] = time() . '.' . $request->image->getClientOriginalExtension();
$request->image->move(base_path() . '/assets/images/users/', $input['image']);
}
Upvotes: 1
Reputation: 154
In your data array you target 'Picture' => $request->$photo,
and not 'Picture' => $photo,
Also it looks like you are pulling just the extension look at the list below of some commands to use and when:
//Display File Name
$file->getClientOriginalName();
//Display File Extension
$file->getClientOriginalExtension();
//Display File Real Path
$file->getRealPath();
//Display File Size
$file->getSize();
//Display File Mime Type
$file->getMimeType();
To Uplaod a file do
$file = $request->file('image');
$file->move($destination, $file->getClientOriginalName());
Upvotes: 1