Created
August 17, 2012 19:25
-
-
Save mikehenriquezf/3381785 to your computer and use it in GitHub Desktop.
File Upload Don't work
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* @Route("/test", name="uploads3") | |
* @Template() | |
*/ | |
public function testAction(){ | |
$document = new FBPost(); | |
$form = $this->createFormBuilder($document) | |
->add('name') | |
->add('file') | |
->getForm() | |
; | |
if ($this->getRequest()->getMethod() === 'POST') { | |
$form->bindRequest($this->getRequest()); | |
if ($form->isValid()) { | |
$em = $this->getDoctrine()->getEntityManager(); | |
$document->upload(); | |
$em->persist($document); | |
$em->flush(); | |
$this->redirect('useradmin'); | |
} | |
} | |
?> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Nobox\SocialShowsBundle\Entity; | |
use Doctrine\ORM\Mapping as ORM; | |
use Symfony\Component\Validator\Constraints as Assert; | |
use Symfony\Component\HttpFoundation\File\UploadedFile; | |
/** | |
* @ORM\Entity | |
* @ORM\HasLifecycleCallbacks | |
*/ | |
class FBPost | |
{ | |
/** | |
* @ORM\Id | |
* @ORM\Column(type="integer") | |
* @ORM\GeneratedValue(strategy="AUTO") | |
*/ | |
public $id; | |
/** | |
* @ORM\Column(type="string", length=255) | |
* @Assert\NotBlank | |
*/ | |
public $name; | |
/** | |
* @Assert\File(maxSize="6000000") | |
*/ | |
public $file; | |
/** | |
* @ORM\Column(type="string", length=255, nullable=true) | |
*/ | |
public $path; | |
/** | |
* @ORM\PrePersist() | |
* @ORM\PreUpdate() | |
*/ | |
public function preUpload() | |
{ | |
if (null !== $this->file) { | |
// do whatever you want to generate a unique name | |
$this->path = uniqid().'.'.$this->file->guessExtension(); | |
} | |
} | |
/** | |
* @ORM\PostPersist() | |
* @ORM\PostUpdate() | |
*/ | |
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->path); | |
unset($this->file); | |
} | |
/** | |
* @ORM\PostRemove() | |
*/ | |
public function removeUpload() | |
{ | |
if ($file = $this->getAbsolutePath()) { | |
unlink($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 setFile(File $file) | |
{ | |
$this->file = $file; | |
} | |
/** | |
* @return \Symfony\Component\HttpFoundation\File\UploadedFile | |
*/ | |
public function getFile() | |
{ | |
return $this->file; | |
} | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment