in my form i have file input where user can upload image,now i want to use this input not only be able to get images but also other type of files such as zip,pdf,txt,doc and docx
Code
my current code supports for images only
//validation
"attachment" => "nullable:image",
//function
if ($request->hasFile('attachment')) {
$attachment = $request->file('attachment');
$filename = 'project-attachment' . '-' . time() . '.' . $attachment->getClientOriginalExtension();
$location = public_path('images/' . $filename);
Image::make($attachment)->resize(1300, 362)->save($location);
$project->attachment = $filename;
}
form
{{Form::file('attachment', array('class' => 'form-control'))}}
Question
- How can I add my other formats support in my code?
- How to make size limits
3 MB?
UPDATE
based on answer below I've made:
//validation
"attachment" => "nullable|mimetypes:image/jpeg,application/pdf,image/gif,image/png,text/plain,application/msword,application/zip|max:3000",
and my function
if ($request->hasFile('attachment')) {
$attachment = $request->file('attachment');
$filename = 'project-attachment' . '-' . time() . '.' . $attachment->getClientOriginalExtension();
$location = public_path('images/' . $filename);
$valid_images = ['image/jpeg','image/gif','image/png'];
if(in_array($_FILES['attachment']['type'], $valid_images)){
Image::make($attachment)->resize(1300, 362)->save($location);
}
$project->attachment = $filename;
}
This code above does upload images and not returning error when i upload for example zip file but the issue is if file isn't image type, it will not store to the host
any idea?