What validation are you looking for on the image usually you check the mime type. Other than that it's pretty standard like any other data, you store the name of the file in the database field.
Image Validation and Upload Tutorials
I am looking for a good tutorial on how to upload input fields with image and validate it all. Can someone redirect()->me to a good one?
Thanks
@artisticre also there are tons of tutorials like https://medium.com/weekly-webtips/how-to-upload-an-image-in-laravel-8-d6ed0551487a
Medium has others as well, but really it's just submitting a form, validating, and saving to database while file is stored on disk, again really routine stuff.
Perhaps you need something like dropzone.
Edit: Also there are several youtube videos on file uploading.
But you also have to figure out is the image name stored in same table, or a related image table. So you would need to setup your relations as well. Is it one file, or are you wanting a tutorial to upload multiple images (files).
If multiple, you would loop over them. Also look at spatie media library.
How did I do?
public function store(Request $request)
{
$validatedData = $request->validate([
'name' => 'required|max:255',
'title' => 'required|max:255',
'email' => 'required',
'phone'=> 'required|max:25',
'bio' => 'required'
]);
$team = new Team;
$team->name = $request->input('name');
$team->title = $request->input('title');
$team->email = $request->input('email');
$team->phone = $request->input('phone');
$team->bio = $request->input('bio');
if ($request->file('image')->isValid()) {
$image = $request->file('image');
$filename = time() . '.' . $image->getClientOriginalExtension();
Image::make($image)->resize(300, 300)->save( public_path('/images/uploads/team/' . $filename) );
}
$team = Team::create($request->all());
return redirect()->route('team.index')->with('success', 'Team Member has been created');
}
You have zero validation around file uploads.
It all works but when I save, the filename for the image saved in the database is
C:\wamp64\tmp\php94DE.tmp
public function store(Request $request)
{
$validatedData = $request->validate([
'name' => 'required|min:3|max:255',
'title' => 'required|max:255',
'email' => 'required',
'phone'=> 'required|max:25',
'bio' => 'required'
]);
$this->validate($validatedData);
$team = new Team;
$team->name = $request->input('name');
$team->title = $request->input('title');
$team->email = $request->input('email');
$team->phone = $request->input('phone');
$team->bio = $request->input('bio');
if($request->hasFile('image'))
{
$destination = 'images/uploads/team/'.$team->image;
if(File::exists($destination)){
File::delete($destination);
}
$image = $request->file('image');
$filename = time() .'.'.$image->getClientOriginalExtension();
Image::make($image)->resize(50,50)->save(public_path('/images/uploads/team/'. $filename));
}
$team = Team::create($request->all());
return redirect()->route('team.index')->with('success', 'Team Member has been created');
}
@artisticre Yes, because you’ve totally disregarded the part of the docs I linked you to that showed you have to deal with an uploaded file in a request.
Read the docs again: https://laravel.com/docs/8.x/requests#storing-uploaded-files
The
storemethod accepts the path where the file should be stored relative to the filesystem's configured root directory. This path should not contain a filename, since a unique ID will automatically be generated to serve as the filename.
@artisticre did you look over a tutorial yet? You need to validate the image as well. Also, I would have thought you would have used examples from documentation without needing a reference.
Yes I forgot to add the image validation. Thank you for the suggestions. This is what I have and still getting the C:\wamp64\tmp\php94DE.tmp as filename in DB
public function store(Request $request)
{
$this->validate($request, [
'name' => 'required|min:3|max:255',
'title' => 'required|max:255',
'image' => 'required|mimes:jpg,jpeg,png,gif |max:4096',
'email' => 'required',
'phone'=> 'required|max:25',
'bio' => 'required'
]);
$team = new Team;
$team->name = $request->input('name');
$team->title = $request->input('title');
$team->email = $request->input('email');
$team->phone = $request->input('phone');
$team->bio = $request->input('bio');
if($request->hasFile('image'))
{
$destination = $request->image->store('images/uploads/team');
if(File::exists($destination)){
File::delete($destination);
}
$image = $request->file('image');
$filename = time() .'.'.$image->getClientOriginalExtension();
Image::make($image)->resize(50,50)->save(public_path('/images/uploads/team/'. $filename));
}
$team = Team::create($request->all());
return redirect()->route('team.index')->with('success', 'Team Member has been created');
}
This is what I have and still getting the C:\wamp64\tmp\php94DE.tmp as filename in DB
@artisticre Read the docs page I’ve sent you twice now…
Store (move) the image.
If you are assigning a name to the file, you need to save it.
$destinationPath = 'place_you_are_saving_to';
Then
$file->move($destinationPath, $filename); you picked that above
Then
$filename gets stored into the database field (you have to do this). Example: mypic.jpg get stored.
https://laravel.com/api/8.x/Illuminate/Filesystem/Filesystem.html#method_move
Also the best tutorial would be the actual source code of how the file is stored:
Laravel uses the symfony components to perform the upload.
I am so lost. I really don't understand what I am doing wrong. Everything is working fine except it still puts the temp file C:wamp64\tmp\phpxxxx.tmp in DB. When I pull in the request like $image = $request->file('image') and then dd($image) I get the following. Please can someone help me figure this out. Everything else is writing to DB correctly except the filename for the image
Illuminate\Http\UploadedFile {#1245 ▼
-test: false
-originalName: "ce.png"
-mimeType: "image/png"
-error: 0
#hashName: null
path: "C:\wamp64\tmp"
filename: "php5203.tmp"
basename: "php5203.tmp"
pathname: "C:\wamp64\tmp\php5203.tmp"
extension: "tmp"
realPath: "C:\wamp64\tmp\php5203.tmp"
aTime: 2021-05-11 20:39:03
mTime: 2021-05-11 20:39:03
cTime: 2021-05-11 20:39:02
inode: 4785074604244482
size: 2410741
perms: 0100666
owner: 0
group: 0
type: "file"
writable: true
readable: true
executable: false
file: true
dir: false
link: false
linkTarget: "C:\wamp64\tmp\php5203.tmp"
public function store(Request $request)
{
$image = $request->file('image');
dd($image);
$this->validate($request, [
'name' => 'required|min:3|max:255',
'title' => 'required|max:255',
'image' => 'required|mimes:jpg,jpeg,png,gif |max:4096',
'email' => 'required',
'phone'=> 'required|max:25',
'bio' => 'required'
]);
if($request->hasFile('image'))
{
$destination = 'images/uploads/team';
if(File::exists($destination)){
File::delete($destination);
}
$image = $request->file('image');
$filename = time() .'.'.$image->getClientOriginalExtension();
$img = Image::make($image)->resize(50,50);
$img->save(public_path('/images/uploads/team/'. $filename));
$newImage = $img;
}
$team = new Team;
$team->name = $request->input('name');
$team->title = $request->input('title');
$team->image = $newImage;
$team->email = $request->input('email');
$team->phone = $request->input('phone');
$team->bio = $request->input('bio');
$team = Team::create($request->all());
return redirect()->route('team.index')->with('success', 'Team Member has been created');
}
@artisticre You’re so “lost” because you’re not reading the frigging documentation that I linked you to twice.
$extension = $request->file('image')->getClientOriginalExtension();
$filename = time().".{$extension}";
$format = in_array($extension, ['jpg', 'png', 'gif', 'tif', 'bmp', 'ico', 'psd', 'webp', 'data-url']) ? $extension : 'jpg';
$image = Image::make($request->file('image'))
->resize(50,50)
->encode($format, 90);
Storage::disk('public')->put($path = "images/uploads/team/{$filename}", (string) $image);
$team = new Team;
// etc
$team->image = $path;
THank you so much. This got rid of the .tmp extension
Please or to participate in this conversation.