I'm building a function to upload multiple images to our website, and so far I have this as my upload form:
<?php
namespace Tyson\AdminBundle\Form;
use Doctrine\ORM\EntityRepository;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
use Symfony\Component\Form\Extension\Core\Type\FileType;
use Symfony\Component\Form\Extension\Core\Type\IntegerType;
class IuType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('id', IntegerType::class);
$builder->add('image', FileType::class, array(
'attr' => array(
'multiple' => 'true',
'data_class' => null
)
));
}
public function getName()
{
return 'iu';
}
}
This is then displayed on the Twig file like this:
<div class="tabs-panel" id="panel3v">
{{ form_start(iu,{action:path('admin_upload-images')}) }}
<div class="row">
<div class="small-3 medium-3 large-3 columns field-label">
<label>Image ID</label>
</div>
<div class="small-9 medium-9 large-9 columns">
{{ form_widget(iu.id) }}
{{ form_errors(iu.id) }}
</div>
</div>
<div class="row">
<div class="small-3 medium-3 large-3 columns field-label">
<label>Image Upload</label>
</div>
<div class="small-9 medium-9 large-9 columns">
{{ form_widget(iu.image) }}
{{ form_errors(iu.image) }}
</div>
</div>
<div class="row">
<div class="small-6 medium-6 large-6 columns">
<button type="submit" class="button" id="saveBtn">Save Changes</button>
</div>
</div>
{{ form_end(iu) }}
</div>
When this is submitted, during testing, I'm outputting what is handed to the controller by the form. This is the output:
[files] => Symfony\Component\HttpFoundation\FileBag Object
(
[parameters:protected] => Array
(
[iu] => Array
(
[image] => Symfony\Component\HttpFoundation\File\UploadedFile Object
(
[test:Symfony\Component\HttpFoundation\File\UploadedFile:private] =>
[originalName:Symfony\Component\HttpFoundation\File\UploadedFile:private] => CnY8dRqXYAAoaAw.jpg
[mimeType:Symfony\Component\HttpFoundation\File\UploadedFile:private] => image/jpeg
[size:Symfony\Component\HttpFoundation\File\UploadedFile:private] => 35164
[error:Symfony\Component\HttpFoundation\File\UploadedFile:private] => 0
[pathName:SplFileInfo:private] => /tmp/phpibZuVW
[fileName:SplFileInfo:private] => phpibZuVW
)
)
)
I could select one image, or 100 images, and still the form is only passing me one image before the controller does anything else.
For reference, this is the code for my controller so far:
<?php
namespace Tyson\AdminBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\Security\Core\SecurityContext;
use Symfony\Component\Security\Core\User\UserInterface;
use Tyson\CoreBundle\Entity\Images;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Cookie;
use Symfony\Component\HttpFoundation\Session\Session;
use Symfony\Component\Form\Form;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\Filesystem\Exception\IOExceptionInterface;
use Symfony\Component\HttpFoundation\File\UploadedFile;
class UploadController extends Controller
{
public function uploadimagesAction(Request $request)
{
$fs = new Filesystem();
$dm = $this->getDoctrine()->getManager();
$dir = '/var/www/html/tyson/web/uploads/images/';
$request = $this->get('request_stack')->getCurrentRequest();
$formdet = $request->request->get('iu');
$imgId = $formdet['id'];
$folderPath = '/var/www/html/tyson/web/uploads/images/'.$imgId;
$imgDirectory = '/var/www/html/tyson/web/uploads/images/'.$imgId.'/';
$folderCheck = $fs->exists($folderPath);
print_r($request);
die();
}
}
I'd imagine that the file
output should contain more than one image to the controller (as the form is submitted once, not repeatedly), so what am I doing wrong for this to happen?