File upload fichier non trouvé
Bonjour,
J'ai un petit soucis avec un formulaire d'upload de fichier, ce dernier m'indique qu'il ne trouve pas le fichier que j'ai sélectionné ??
Si je supprime ce champ, l'enregistrement s'effectue bien pour les autres données (nom, prénom...)
J'ai simplifié mon code pour que ce soit plus clair :
Mon buildForm :
Code:
->add('image','file')
Dans ma vue Twig :
Code:
1 2 3
| {{ form_label(form.image) }}
{{ form_errors(form.image) }}
{{ form_widget(form.image) }} |
Dans mon controller :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
| public function editerAction($id = null)
{
$message='';
$em = $this->container->get('doctrine')->getEntityManager();
if (isset($id))
{
// modification dun profil existant : on recherche ses données
$profil= $em->find('MyAppFilmothequeBundle:Profil', $id);
if (!$profil)
{
$message='Aucun profil trouvé';
}
}
else
{
// ajout dun nouveau profil
$profil= new Profil();
}
$form = $this->container->get('form.factory')->create(new ProfilForm(), $profil);
$request = $this->container->get('request');
if ($request->getMethod() == 'POST')
{
$form->bindRequest($request);
if ($form->isValid())
{
$em->persist($profil);
$em->flush();
if (isset($id))
{
$message = $this->container->get('translator')->trans('profil.modifier.succes',array(
'%nom%' => $profil->getNom(),
'%prenom%' => $profil->getPrenom()
));
}
else
{
$message = $this->container->get('translator')->trans('profil.ajouter.succes',array(
'%nom%' => $profil->getNom(),
'%prenom%' => $profil->getPrenom()
));
}
}
}
return $this->container->get('templating')->renderResponse(
'MyAppFilmothequeBundle:Profil:editer.html.twig',
array(
'form' => $form->createView(),
'message' => $message,
'profil' => $profil
)
);
} |
Et dans mon entité, j'ai mis de quoi gérer l'upload de fichier :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
| <?php
namespace MyApp\FilmothequeBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
/**
* @orm\Entity
* @ORM\HasLifecycleCallbacks
*/
Class Profil
{
......................
/**
* @orm\Column(type="string", length="255")
* @Assert\File( maxSize = "1024k", mimeTypesMessage = "Please upload a valid Image")
*/
protected $image;
public function getFullImagePath() {
return null === $this->image ? null : $this->getUploadRootDir(). $this->image;
}
protected function getUploadRootDir() {
// the absolute directory path where uploaded documents should be saved
return $this->getTmpUploadRootDir().$this->getId()."/";
}
protected function getTmpUploadRootDir() {
// the absolute directory path where uploaded documents should be saved
return __DIR__."/../Resources/images/";
}
/**
* @ORM\PrePersist()
* @ORM\PreUpdate()
*/
public function uploadImage() {
// the file property can be empty if the field is not required
if (null === $this->image) {
return;
}
if(!$this->id){
$this->image->move($this->getTmpUploadRootDir(), $this->image->getClientOriginalName());
}else{
$this->image->move($this->getUploadRootDir(), $this->image->getClientOriginalName());
}
$this->setImage($this->image->getClientOriginalName());
}
/**
* @ORM\PostPersist()
*/
public function moveImage()
{
if (null === $this->image) {
return;
}
if(!is_dir($this->getUploadRootDir())){
mkdir($this->getUploadRootDir());
}
copy($this->getTmpUploadRootDir().$this->image, $this->getFullImagePath());
unlink($this->getTmpUploadRootDir().$this->image);
}
/**
* @ORM\PreRemove()
*/
public function removeImage()
{
unlink($this->getFullImagePath());
rmdir($this->getUploadRootDir());
} |
Merci par avance pour votre aide :)