I have page, where users will upload pictures. These pictures will be all loaded after opening this page, if the form is submited, it will save the picture file and load it with the rest.
Going through the official Symfony documentation for tens times this week I cannot figure out why my function homeView() does nothing.
What I really want is to have this file saved in /public/pictures directory but it doesn't happen.
The website is just refreshing giving me no error message or anything. Am I doing something wrong?
HomeController file:
<?php
namespace App\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Form\Extension\Core\Type\FileType;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
use Symfony\Component\HttpFoundation\File\Exception\FileException;
use Symfony\Component\HttpFoundation\Response;
use App\Service\PicturesService;
use App\Entity\Picture;
use Symfony\Component\HttpFoundation\File\UploadedFile;
class HomeController extends AbstractController
{
public function homeView()
{
$pictures = $this->getDoctrine()
->getRepository(Picture::class)
->findAll();
$form = $this->createFormBuilder()
->add('file', FileType::class)
->add('save', SubmitType::class)
->getForm();
if ($form->isSubmitted() && $form->isValid()) {
$formData = $form->getData();
try {
$formData->file->move(
$this->getParameter('pictures_directory'),
'file_name');
}
catch (FileException $e) {
return new Response($e);
}
/*
$entityManager = $this->getDoctrine()->getManager();
$entityManager->persist($db_file_url);
$entityManager->flush();
*/
}
return $this->render('home.html.twig', array(
'form' => $form->createView(),
'pictures' => $pictures));
}
}
services.yaml file:
parameters:
locale: 'en'
pictures_directory: '%kernel.project_dir%/public/pictures'
services:
_defaults:
autowire: true
autoconfigure: true
App\:
resource: '../src/*'
exclude: '../src/{DependencyInjection,Entity,Migrations,Tests,Kernel.php}'
App\Controller\:
resource: '../src/Controller'
tags: ['controller.service_arguments']
twig view form:
{{ form(form) }}
{{ form_widget(form) }}
{{ form_end(form) }}
sudo chmod 777 /public/picturesespecially for this directory.catchblock in your code?