1
votes

i am trying to upload a form with a file field. the form loads ok but the Symfony\Component\HttpFoundation\Request object of the request only represents the file as a string instead of an UploadedFile object.

use Symfony\Component\HttpFoundation\Request;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;

use some\Bundle\Entity\Images;
use some\Bundle\Form\ImagesType;




/**
 * Images controller.
 *
 */
class ImagesController extends Controller {    
public function createAction(Request $request) {
            $entity = new Images();

            $form = $this->createForm(new ImagesType(), $entity);

            $form->bind($request);
            if ($form->isValid()) {                 

                $em = $this->getDoctrine()->getManager();
                $em->persist($entity);
                $em->flush();

                return $this->redirect($this->generateUrl('images_show', array('id' => $entity->getId())));
            }

            return $this->render('sephaBundle:admin/Images:new.html.twig', array(
                        'entity' => $entity,
                        'form' => $form->createView(),
                    ));
       } 
}

the entity class

<?php

namespace some\Bundle\Entity;

use Doctrine\ORM\Mapping as ORM;

use some\Bundle\Entity\Images;

/**
 * some\Bundle\Entity\Images
 */
class Images {

    /**
     * @var integer $id
     */
    private $id;

    /**
     * @var string $link
     */
    private $link;

    /**
     * @var string $name
     */
    private $name;

    /**
     * @var file $file
     */
    public $file;

     public function getFile() {
        return $this->file;
    }
    public function setFile($file) {
        $this->file = $file;

        return $this;
    }
    /**
     * Get id
     *
     * @return integer 
     */
    public function getId() {
        return $this->id;
    }

    /**
     * Set link
     *
     * @param string $link
     * @return Images
     */
    public function setLink($link) {
        $this->link = $link;

        return $this;
    }

    /**
     * Get link
     *
     * @return string 
     */
    public function getLink() {
        return $this->link;
    }

    /**
     * Get $Name
     *
     * @return string
     */
    public function getName() {
        return $this->name;
    }

    /**
     * Set Name
     *
     * @param string $name
     * @return Images
     */
    public function setName($name) {
        $this->name = $name;

        return $this;
    }

///////////////////////////////////////////////////////////////
    // <editor-fold defaultstate="collapsed" desc="process file storage location">

    public function getAbsoluteLink() {
        return null === $this->link ? null : $this->getUploadRootDir() . '/' . $this->link;
    }

    public function getWebLink() {
        return null === $this->link ? null : $this->getUploadDir() . '/' . $this->link;
    }

    protected function getUploadRootDir() {
        // the absolute directory link where uploaded
        // documents should be saved
        return __DIR__ . '/../../../../web/' . $this->getUploadDir();
    }

    protected function getUploadDir() {
        // get rid of the __DIR__ so it doesn't screw up
        // when displaying uploaded doc/image in the view.
        return 'uploads/documents';
    }

// </editor-fold>
///////////////////////////////////////////////////////////////

    public function preUpload() {
        if (null !== $this->file) {
            // do whatever you want to generate a unique name

            $filename = sha1(uniqid(mt_rand(), true));

            $this->link = $filename . '.' . $this->file->guessExtension();****//error here $this->file is a string instead of UploadedFile object**** 
        }
    }


    public function upload() {
//        if (null === $this->file) {
//            return;
//        }
        // if there is an error when moving the file, an exception will
        // be automatically thrown by move(). This will properly prevent
        // the entity from being persisted to the database on error
        $this->file->move($this->getUploadRootDir(), $this->link);

        unset($this->file);
    }

    /**
     * //@ ORM\PostRemove()
     */
    public function removeUpload() {
        if ($file === $this->getAbsoluteLink()) {
            unlink($file);
        }
    }

}?>

the form generation class

namespace some\Bundle\Form;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
use Symfony\Component\HttpFoundation\File\UploadedFile;

class ImagesType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('Name')
            ->add('file','file',array(
                        'label'=>'Image',
                        'data_class' => 'Symfony\Component\HttpFoundation\File\UploadedFile'
                        ));
    }

    public function setDefaultOptions(OptionsResolverInterface $resolver)
    {
        $resolver->setDefaults(array(
            'data_class' => 'sepha\Bundle\Entity\Images'
        ));
    }

    public function getName()
    {
        return 'sepha_bundle_imagestype';
    }
}

the ORM MAP in yaml

some\Bundle\Entity\Images:
    type: entity
    table: images
    fields:
        id:
            id: true
            type: integer
            unsigned: false
            nullable: false
            generator:
                strategy: IDENTITY
        name:
            type: string
            length: 20
            fixed: false
            nullable: false
        link:
            type: string
            length: 60
            fixed: false
            nullable: false

    properties:
        file:
          - Image:
              maxSize: 1024k
              mimeTypes: [image/jpeg, image/png, image/gif ]
              mimeTypesMessage: Please upload a valid image of type jpeg,png or gif

    lifecycleCallbacks:
      prePersist: [preUpload]
      preUpdate: [preUpload]
      postPersist: [upload]
      postUpdate: [upload]
      postRemove: [removeUpload]

what should i do to make the request objet to represent the file request as an UploadedFile object and not a string

2
When submitting data the client sends the file path as string to your project which then takes this path to upload and process the file.Upvote

2 Answers

4
votes

Do you have the form_enctype(form) directive in the form tag of your template?

ie.

<form action="{{ path('your_path') }}" {{ form_enctype(form) }} method="POST">

UPDATE

Your class proterties (for your entities) should all be protected (with getter and setter). There is no point to have them private.

protected $file;

2
votes

You have to retrieve the file from the request.

// retrieves an instance of UploadedFile identified by foo
$request->files->get('foo');

http://symfony.com/doc/current/book/http_fundamentals.html