I have a simple Drupal 8 form where users can upload a picture.
The issue is that the upload_max_filesize
is set to 2M on the server. So if I upload a bigger file, the form submission fails:
Fatal error: Call to a member function save() on a non-object
Here is what I have tried in the submitForm:
$validators = array(
'file_validate_is_image' => array(),
'file_validate_size' => array(1024 * 1024),
);
/** @var File[] $pictures */
$pictures = file_save_upload('picture_upload', $validators, 'public://');
if(null !== $pictures) {
/** @var File $picture */
$picture = reset($pictures);
$picture->save();
}
This actually leads to 2 issues when trying to upload a large .jpg
image:
- The file could not be saved because it exceeds 2 MB, the maximum allowed size for uploads.
- Image type not supported. Allowed types: png jpeg gif
So my questions are: why does the validator not accept a .jpg image? How would I go about avoiding the fatal error and return to the form if the file size is too big?
Edit:
I have changed my validator to this now:
$validators = array(
'file_validate_extensions' => array('gif png jpg jpeg'),
'file_validate_size' => array(2 * 1024 * 1024), // 2MB file size limit
);
It works now in the way that it accepts my .jpg
image and doesn't accept files larger than 2MB. But how would I go about returning to the form with an error message instead of going straight to the PHP fatal error?