1

I'm working with a bundle to upload any type of file.
For this purpose, I created a new bundle uploadBundle with the following files:

Entity "Archivo.php"

<?php

namespace SisEvo\UploadBundle\Entity;


use Symfony\Component\HttpFoundation\File\File;
use Symfony\Component\Validator\Constraints as Assert;
use Doctrine\ORM\Mapping as ORM;

/**
* @ORM\Entity()
*/
class Archivo {

/**
 * @ORM\Id
 * @ORM\Column(type="integer")
 * @ORM\GeneratedValue(strategy="AUTO")
 */
private $id;

/**
 * @ORM\Column(type="integer")
 */
private $idTipo;

/**
 * @ORM\Column(type="integer")
 */
private $idAgregado;

/**
 * @var File
 *
 * @Assert\File(
 *     maxSize = "50M",
 *     mimeTypes = {"application/pdf", "application/x-pdf, application/x-rar-compressed, application/octet-stream, application/csv, application/vnd.openxmlformats-officedocument.wordprocessingml.document, application/vnd.openxmlformats-officedocument.spreadsheetml.sheet, application/msword, application/vnd.ms-excel"},
 *     maxSizeMessage = "El arxiu es massa gran",
 *     mimeTypesMessage = "El tipus d'arxiu seleccionat no està permitit" 
 * )
 */
protected $file;

Additionally, this entity contains get and setrequired methods, and finally, an upload method that looks like this:

public function upload($file, $path = '%kernel.root_dir%/../web/uploads') {
    if (null === $file) {
        return;
    }
    $name = $file->getClientOriginalName();
    $file->move(
            $path, $name
    );
    unset($file);
}

Form "ArchivoType.php"

<?php

namespace SisEvo\UploadBundle\Form;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Bridge\Doctrine\Form\Type\EntityType;


class ArchivoType extends AbstractType {

/**
 * @param FormBuilderInterface $builder
 * @param array $options
 */
    public function buildForm(FormBuilderInterface $builder, array $options) {
    $builder
            ->add('file', 'file')

    ;
}

/**
 * @param OptionsResolver $resolver
 */
public function configureOptions(OptionsResolver $resolver) {
    $resolver->setDefaults(array(
        'data_class' => 'SisEvo\UploadBundle\Entity\Archivo'
    ));
}


}

Controller "UploadController.php"

class UploadController extends Controller {

    /**
     * @Route("/file_upload/" , name = "file_upload")
     */
    public function probarAction() {
        $request = $this->getRequest();

        $file = new Archivo();
        $formulario = $this->createForm(new ArchivoType());

        $formulario->handleRequest($request);

        if ($formulario->isValid()) {
            $em = $this->getDoctrine()->getManager();
            $data = $formulario->getData();
            $fileUploaded = $data->getFile();
            $file->upload($fileUploaded);

            echo "SUCCESS!";
        }
        return $this->render("upload/prova.html.twig"
                    , array("formulario" => $formulario->createView())
        );
    }

}

The problem

If the folder /uploads doesn't exists, this code works fine; this is, the first time that I use it, but the following times Symfony shows this error:

Unable to create the "%kernel.root_dir%/../web/uploads" directory

And the error is produced in this piece of code in vendor/symfony/symfony/src/Symfony/Component/HttpFoundation/File/File.php at line 110

if (!is_dir($directory)) {
    if (false === @mkdir($directory, 0777, true) && !is_dir($directory)) {
        throw new FileException(sprintf('Unable to create the "%s" directory', $directory));
    }
} elseif (!is_writable($directory)) {
    throw new FileException(sprintf('Unable to write in the "%s" directory', $directory));

It's seems like Symfony doesn't find uploads folders. I tried to change permisions with chmod 777 -R, but the issue persists. Any idea to solve this? Thanks in advance :)

EDIT with more infomation

First
I am creating a service to upload files from any part of my application, so I created a bundle with an entity and a simple form. The controller post above it's only a proof, and when the uploadBundle will been finished, this controller will be deleted and the upload method will be used in each part of the application that I need it.
Furthermore, the code than store the information of each uploaded file in the database are not developed yet.

Please, can you explain me why is wrong my architecture? Thanks :)

Second
Chmod gets the octal 777 from the symfony kernel; code posted in the problem section is from symfony.

Third

You are right, the code doesn't works how as I expected. It create a folder inside web folder like this: web/web/uploads. I need to get more information about internal folders on Symfony. Anyway, @pablo 's answer solve my problem with this.

Isaac Bosca
  • 1,588
  • 1
  • 15
  • 34
  • **First of all**: you have bad architecture. Don't put domain logic into Entity. **Second**: `chmod` gets the octal number as a parameter so you need to pass `0777` to it. **Third**: are you sure that exactly this code works fine? And this code make the folder? Why am I asking? Because I think that your `File` instance doesn't know anything about your Container and so it doesn't know what is it `%kernel.root_dir%` – Michael Sivolobov Jul 26 '16 at 16:42
  • Hi @MichaelSivolobov , I apreciate your comment; for this reason I need more information to improve my code, now I will reply each part of your comment in the principal post; if you can, tell me why am I wrong. Thank you in advance. – Isaac Bosca Jul 27 '16 at 07:49

1 Answers1

3

The way that you're getting the $path (kernel.root_dir) is wrong

Try this approach:

public function probarAction() {
    $request = $this->getRequest();

    $file = new Archivo();
    $formulario = $this->createForm(new ArchivoType());

    $formulario->handleRequest($request);

    if ($formulario->isValid()) {
        $em = $this->getDoctrine()->getManager();
        $data = $formulario->getData();
        $fileUploaded = $data->getFile();
        $path = $this->get('kernel')->getRootDir() . '/../web/uploads';
        $file->upload($fileUploaded, $path);

        echo "SUCCESS!";
    }
    return $this->render("upload/prova.html.twig"
                , array("formulario" => $formulario->createView())
    );
}

Please let us know if you have some progress

Greetings

Added the documentation links based on @Isaac comment:

A more efficient approach to get the $path:

$this->getParameter('kernel.root_dir') . '/../web/uploads';
Pablo Rodriguez V.
  • 358
  • 1
  • 4
  • 9
  • Hi @pablo , your sollution works for me, thank you. Seems like my way to get the `root dir` was wrong. Thank you!! Please, can you post any offcial documentation about these methods `$this->get('kernel')->getRootDir()`. Thank you very much :) – Isaac Bosca Jul 27 '16 at 07:43
  • Done, any issues please notify – Pablo Rodriguez V. Jul 27 '16 at 15:01