Hi there!
I'm having trouble with file upload...
When i try the doc example in the acme demo Bundle it works but when i
try to use it in my admin bundle i get the following error :
[code]The file could not be found[/code]
Although the file choosed in my computer was the same as in the
demoBundle i even used the same entity (use Acme/demoBundle/entity/
Document)
I suspect that it is because for other entities i use yml... or maybe
it is because there are other forms on the page...
So here the code... (I've deleted parts that were not interesting such
as validation of other forms)
[code]<?php
//Doctrine
$em = $this->getDoctrine()->getEntityManager();
$products = $em->getRepository('AcmeAdminBundle:Upload')-
>findAll();
$cats = $em->getRepository('AcmeAdminBundle:UploadCat')-
>findAll();
//fin des requètes
$options = array();//utile pour les catégories du formulaire
d'upload
foreach ($cats as $cat) {
$options[$cat->getId()] = $cat->getName();
}
//Forms
//Upload cat create
$catCreateObj = new UploadCat();
$catCreate = $this->createFormBuilder($catCreateObj)
->add('name')
->getForm();
$catUpdate = $this->createFormBuilder($catCreateObj)
->add('name')
->getForm();
$catsForm = array($catCreate->createView(), $catUpdate-
>createView());
//Speedbarre
$speed = new Speedbarre();
$speed = $this->createFormBuilder($speed)
->add('name')
->add('link');
//Upload form
$document = new Document();
$form = $this->createFormBuilder($document)
->add('file', 'file')
->add('visible', 'choice', array(
'choices' => array('' => 'Visible sur la
gallerie du site','1' => 'Oui', '0' => 'Non')))
->add('name')
->add('cat', 'choice', array('choices' => $options))
->getForm();
//fin des formulaires
if( $request->getMethod() == 'POST' or $this->getRequest()-
>attributes->get('action') == 'delete' and $request-
>isXmlHttpRequest() !== TRUE )
{
$action = $request->attributes->get('action');
$em = $this->getDoctrine()->getEntityManager();
$error = array(FALSE);
switch($action){
case 'new':
$form->bindRequest($this->getRequest());
if ($form->isValid()) {
$em = $this->getDoctrine()-
>getEntityManager();
$document->upload();
$em->persist($document);
$em->flush();
//$this->redirect($this-
>generateUrl('UploadA'));
}else{ $error = $form;}
break;
}
if(isset($good) != 0)return new RedirectResponse($this-
>generateUrl('Admin_gallery'));
else if($request->getMethod() == 'POST') {
return $this-
>render('AlshAdminBundle:Gallery:error.html.twig', array(
'error' => $error->createView()));
}
}
[/code]
and here goes the document entity :
[code]<?php
// src/Acme/DemoBundle/Entity/Document.php
namespace Acme\DemoBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
/**
* @ORM\Entity
*/
class Document
{
/**
* @var integer $id
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @var string $name
*
* @ORM\Column(name="name", type="string", length=255)
*/
private $name;
/**
* @var string $type
*
* @ORM\Column(name="type", type="string", length=255)
* @Assert\MinLength(3)
*/
private $type;
/**
* @var string $cat
*
* @ORM\Column(name="cat", type="string", length=255)
* @Assert\MaxLength(10)
*/
private $cat;
/**
* @var boolean $visible
*
* @ORM\Column(name="visible", type="boolean")
*/
private $visible;
/**
* @var string $path
*
* @ORM\Column(name="path", type="string", length=255)
*/
private $path;
/**
* @Assert\File(maxSize="6000000")
*/
public $file;
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 when displaying
uploaded doc/image in the view.
return 'uploads/documents';
}
public function upload()
{
// the file property can be empty if the field is not required
if (null === $this->file) {
return;
}
// we 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->file->move($this->getUploadRootDir(), $this->file-
>getClientOriginalName());
// set the path property to the filename where you'ved saved
the file
$this->setPath($this->file->getClientOriginalName());
// clean up the file property as you won't need it anymore
$this->file = null;
}
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set name
*
* @param string $name
*/
public function setName($name)
{
$this->name = $name;
}
/**
* Get name
*
* @return string
*/
public function getName()
{
return $this->name;
}
/**
* Set type
*
* @param string $type
*/
public function setType($type)
{
$this->type = $type;
}
/**
* Get type
*
* @return string
*/
public function getType()
{
return $this->type;
}
/**
* Set cat
*
* @param string $cat
*/
public function setCat($cat)
{
$this->cat = $cat;
}
/**
* Get cat
*
* @return string
*/
public function getCat()
{
return $this->cat;
}
/**
* Set visible
*
* @param boolean $visible
*/
public function setVisible($visible)
{
$this->visible = $visible;
}
/**
* Get visible
*
* @return boolean
*/
public function getVisible()
{
return $this->visible;
}
/**
* Set path
*
* @param string $path
*/
public function setPath($path)
{
$this->path = $path;
}
/**
* Get path
*
* @return string
*/
public function getPath()
{
return $this->path;
}
}
[/code]
Thanks for your help!
--
If you want to report a vulnerability issue on symfony, please send it to
security at symfony-project.com
You received this message because you are subscribed to the Google
Groups "symfony developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/symfony-devs?hl=en