I have file field with required = false, multiple=multiple. Everything works fine, but if file input is empty, symfony2 show me this error.
Error: Call to a member function guessExtension() on null
Here is my form type:
->add('file', 'file', array(
'multiple' => 'multiple',
'data_class' => null,
'required' => false,
'mapped' => false,
'attr' => array(
'maxSize' => '1024k',
'accept' => 'image/*',
)
))
Upload function:
private function uploadImg($files)
{
$path = array();
foreach ($files as $file) {
$extension = $file->guessExtension();
if (!$extension) {
$extension = 'bin';
}
$randomName = 'image' . date('Y-m-d-H-i-s') . uniqid() . '.' . $extension;
$file->move('uploads/images/', $randomName);
$path[] = $randomName;
}
return $path;
}
How can I check if file input is empty?
$form['file']->getData(); every time returns Array
count. Sincemultipleis set totrueyou will always end up receiving an array, whether it's empty or not.