Reputation: 375
I'm making an API for uploading images. I want to retrieve the file type of the image, for example, the image is .png
or .jpeg
and other types of images.
date_default_timezone_set("Asia/Jakarta");
$date_now = date("Y-m-d H:i:s");
$uploadFolder = '/images/all/';
$file_name = uniqid().'.png';
if(!$request->hasFile('image')) {
return response()->json([
'success' => false,
'data' => null,
'message' => 'upload_file_not_found',
], 401);
}
$file = $request->file('image');
if(!$file->isValid()) {
return response()->json([
'success' => false,
'data' => null,
'message' => 'invalid_file_upload',
], 401);
}
$path = public_path() . $uploadFolder;
$file->move($path, $file_name);
$employee = Employee::find($id);
$employee->photo_file_path = $uploadFolder.$file_name;
$employee->photo_file_name = $file->getClientOriginalName();
// $employee->photo_file_type =
$employee->modified = $date_now;
This is my code.
Upvotes: 1
Views: 169
Reputation: 1340
You Can Try with This.
$file = $request->file('image');
$extension = $file->getClientOriginalExtension();
$mimeType = $file->getMimeType();
Upvotes: 2
Reputation: 600
You can get the extension of file to determine which type of file it is.
$type = $request->file->extension();
This way you can find the extension of file. ex. .jpeg, .png, etc.
Upvotes: 1