0

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) }}
6
  • Does your www group or user have write permissions to the /public/pictures directory? Commented Jan 4, 2019 at 0:27
  • @Strom Yes, I did sudo chmod 777 /public/pictures especially for this directory. Commented Jan 4, 2019 at 0:33
  • What does Symfony tell you when you remove the catch block in your code? Commented Jan 4, 2019 at 0:34
  • 1
    I guess your code does not enter the first if statement on account of not being valid and/or submitted. You should check that first. Commented Jan 4, 2019 at 0:51
  • 1
    Never mark a web accessible directory as both read and write. Mark the target directory as 270(write only(www user),all for group (create a group for the cron script to run as), no world). Use a cron job(as user that is a member of the new group) to verify it is a valid image file (use the file command) and use imagemagick to re-encode the image file to further validate the file, then copy the file to a public directory with permissions 5,7,0(owned by www, new group). On any failure, delete the file. Commented Jan 4, 2019 at 1:51

1 Answer 1

1

Firstly your code will never reach the inner code of the first if statement in your controller due to the form not being submitted. Add the following line below the form instantiation:

$form->handleRequest($request);

For the form handling to work you need to type hint the Symfony\Component\HttpFoundation\Request object as an argument in your homeView() method:

public function homeView(Symfony\Component\HttpFoundation\Request $request)

Now that your form is being submitted you will encounter the following error:

Notice: Trying to get property 'file' of non-object

That error occurs because the getData() method of your $form object returns an array, not an object. You need to access the file element of this array and call the move() method on that:

$formData = $form->getData();

/** @var UploadedFile $file */
$file = $formData['file'];
$file->move('var/files', 'file_name');
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you a lot!!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.