Im trying to Handle File Uploads with Doctrine but whene i check Request POST Parameters in profiler
[
prix => 50,
description => test test,
city => 1,
nbrLivres => 5,
livre => [
titre => titre du livre,
isbn => 1111111110,
dateEdition => [
month => 1,
day => 1,
year => 2010
],
langue => francais,
auteur => wail,
nbrPages => 4,
etatLivre => 1,
typeOuvrage => 1,
genre => 1
],
save => ,
_token => gw-x78-gqUWxu5suZ8thFrvTFwUjpbxAfhzrcX_PQgs
]
I don't find file value it's was not send.
My Entity Class:
class Entity{
....
/**
* @ORM\Column(type="string", length=255, nullable=true)
*/
public $path;
public function getAbsolutePath()
{
return null === $this->path
? null
: $this->getUploadRootDir().'/'.$this->path;
}
public function getWebPath()
{
return null === $this->path
? null
: $this->getUploadDir().'/'.$this->path;
}
protected function getUploadRootDir()
{
// the absolute directory path 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';
}
/**
* @Assert\File(maxSize="6000000")
*/
private $file;
/**
* Sets file.
*
* @param UploadedFile $file
*/
public function setFile(UploadedFile $file = null)
{
$this->file = $file;
}
/**
* Get file.
*
* @return UploadedFile
*/
public function getFile()
{
return $this->file;
}
public function upload()
{
// the file property can be empty if the field is not required
if (null === $this->getFile()) {
return;
}
// use the original file name here but you should
// sanitize it at least to avoid any security issues
// move takes the target directory and then the
// target filename to move to
$this->getFile()->move(
$this->getUploadRootDir(),
$this->getFile()->getClientOriginalName()
);
// set the path property to the filename where you've saved the file
$this->path = $this->getFile()->getClientOriginalName();
// clean up the file property as you won't need it anymore
$this->file = null;
}
}
I Follow the tutorial from symfony2 documentation but without success , any idea ?
Form class:
<?php
namespace AppBundle\Form;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
class LivreType extends AbstractType
{
/**
* @param FormBuilderInterface $builder
* @param array $options
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('titre')
->add('isbn')
->add('dateEdition', 'date')
->add('langue')
->add('auteur')
->add('imageFile', 'file')
;
}
/**
* @param OptionsResolverInterface $resolver
*/
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'AppBundle\Entity\Livre'
));
}
/**
* @return string
*/
public function getName()
{
return 'appbundle_livre';
}
}
In Twig file
{{ form_start(depot, {'action': path('persistbook'), 'method': 'POST'}) }}
{{ form_widget(depot) }}
{{ form_end(depot) }}