You can ensure that user is sending a proper file image with the below:
public function addInputFilter()
{
$inputFilter = new InputFilter\InputFilter();
// File Input
$fileInput = new InputFilter\FileInput('image-file');
$fileInput->setRequired(true);
// Define validators and filters as if only one file was being uploaded.
// All files will be run through the same validators and filters
// automatically.
$fileInput->getValidatorChain()
->attachByName('filesize', ['max' => 204800])
->attachByName('filemimetype', ['mimeType' => 'image/png,image/x-png'])
->attachByName('fileimagesize', ['maxWidth' => 100, 'maxHeight' => 100]);
// All files will be renamed, e.g.:
// ./data/tmpuploads/avatar_4b3403665fea6.png,
// ./data/tmpuploads/avatar_5c45147660fb7.png
$fileInput->getFilterChain()->attachByName(
'filerenameupload',
[
'target' => './data/tmpuploads/avatar.png',
'randomize' => true,
]
);
$inputFilter->add($fileInput);
$this->setInputFilter($inputFilter);
}
A list of Input Filters is here:
- Count
- crc32
- ExcludeExtension
- ExcludeMimeType
- Exists
- Extension
- FilesSize
- Hash
- ImageSize
- IsCompressed
- IsImage
- Md5
- MimeType
- NotExists
- Sha1
- Size
- Upload
- UploadFile
- WordCount
Docs: https://docs.laminas.dev/laminas-validator/validators/file/intro/