php – 如何使用symfony2“手动”处理文件上传?
发布时间:2020-12-13 18:04:39 所属栏目:PHP教程 来源:网络整理
导读:因为我有一个自定义构建的jQuery插件来将文件上传传递给我的symfony2 webapp,我正在寻找在控制器中处理此上传的方法. 我目前拥有的标准(非ajax)文件上传(并且适用于同步调用)看起来像这样 控制器节选 ... $entity = new Image(); $request = $this-getReques
因为我有一个自定义构建的jQuery插件来将文件上传传递给我的symfony2 webapp,我正在寻找在控制器中处理此上传的方法.
我目前拥有的标准(非ajax)文件上传(并且适用于同步调用)看起来像这样 控制器节选 ... $entity = new Image(); $request = $this->getRequest(); $form = $this->createForm(new ImageType($createAction),$entity); $form->bind($request); // <-- Find a way to make this connection manually?! //check that a file was chosen $fileExists = isset($entity->file); if ( ($form->isValid()) && ($fileExists) ) { $em = $this->getDoctrine()->getManager(); $em->persist($entity); $em->flush(); } ... 表单类型:表单只接受文件和名称: class ImageType extends AbstractType { ... public function buildForm(FormBuilderInterface $builder,array $options) { $createAction = $this->createAction; if ($createAction) { $builder ->add('file') ; } $builder ->add('name','text',array('label' => 'Namn')) ; } ... } 正如我所理解的(或者说显然不明白)使用symfony2和doctrine的文件上传系统,在这个调用的引擎盖下有相当多的魔力 $form->bind($request); 例如,如果我跳过这个bind(),而是尝试像这样手动创建Image实体…… $request = $this->getRequest(); $parent = $request->request->get('parent'); $file = $request->request->get('file1'); $name = $request->request->get('name'); $entity->setName( $name ); $entity->setFile( $file ); $entity->setFolder( null ); …我发现它甚至没有setFile(),因此以其他方式处理.这是Image实体: namespace BizTVMediaManagementBundleEntity; use DoctrineORMMapping as ORM; use SymfonyComponentValidatorConstraints as Assert; /** * BizTVMediaManagementBundleEntityImage * * @ORMTable(name="image") * @ORMEntity * @ORMHasLifecycleCallbacks */ class Image { /** * @var integer $id * * @ORMColumn(name="id",type="integer") * @ORMId * @ORMGeneratedValue(strategy="AUTO") */ private $id; /** * @var string $name * * @ORMColumn(name="name",type="string",length=255) * @AssertNotBlank(message = "Bilden m?ste ha ett namn") */ private $name; /** * @var integer $width * * @ORMColumn(name="width",type="integer") */ private $width; /** * @var integer $height * * @ORMColumn(name="height",type="integer") */ private $height; /** * @ORMColumn(type="string",length=255,nullable=true) */ private $path; //The deleteRequested variable is to flag that an image has been deleted by user. //Due to slideshow issues we can however not delete the image right away,we can't risk to remove it from the //cache manifest before the slideshow round is up. /** * @var time $deleteRequested * * @ORMColumn(name="delete_requested",type="datetime",nullable=true) */ private $deleteRequested; /** * @var object BizTVBackendBundleEntitycompany * * @ORMManyToOne(targetEntity="BizTVBackendBundleEntitycompany") * @ORMJoinColumn(name="company",referencedColumnName="id",nullable=false) */ protected $company; /** * @var object BizTVMediaManagementBundleEntityFolder * * @ORMManyToOne(targetEntity="BizTVMediaManagementBundleEntityFolder") * @ORMJoinColumn(name="folder",nullable=true) */ protected $folder; /** * @AssertFile(maxSize="12000000") */ public $file; /** * @ORMOneToOne(targetEntity="BizTVMediaManagementBundleEntityQrImage",mappedBy="image") */ protected $qr; /** * @ORMPrePersist() * @ORMPreUpdate() */ public function preUpload() { if (null !== $this->file) { // do whatever you want to generate a unique name $this->path = sha1(uniqid(mt_rand(),true)).'.'.$this->file->guessExtension(); } } /** * @ORMPostPersist() * @ORMPostUpdate() */ 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); } /** * @ORMPostRemove() */ 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/images/'.$this->getCompany()->getId(); } /** * 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 width * * @param integer $width */ public function setWidth($width) { $this->width = $width; } /** * Get width * * @return integer */ public function getWidth() { return $this->width; } /** * Set height * * @param integer $height */ public function setHeight($height) { $this->height = $height; } /** * Get height * * @return integer */ public function getHeight() { return $this->height; } /** * Set path * * @param string $path */ public function setPath($path) { $this->path = $path; } /** * Get path * * @return string */ public function getPath() { return $this->path; } /** * Set company * * @param BizTVBackendBundleEntitycompany $company */ public function setCompany(BizTVBackendBundleEntitycompany $company) { $this->company = $company; } /** * Get company * * @return BizTVBackendBundleEntitycompany */ public function getCompany() { return $this->company; } /** * Set folder * * @param BizTVMediaManagementBundleEntityFolder $folder */ public function setFolder(BizTVMediaManagementBundleEntityFolder $folder = NULL) { $this->folder = $folder; } /** * Get folder * * @return BizTVMediaManagementBundleEntityFolder */ public function getFolder() { return $this->folder; } /** * Set qr * * @param BizTVMediaManagementBundleEntityQrImage $qr */ public function setQr(BizTVMediaManagementBundleEntityQrImage $qr = null) { $this->qr = $qr; } /** * Get qr * * @return BizTVMediaManagementBundleEntityQrImage */ public function getQr() { return $this->qr; } /** * Set deleteRequested * * @param date $deleteRequested */ public function setDeleteRequested($deleteRequested = null) { $this->deleteRequested = $deleteRequested; } /** * Get deleteRequested * * @return date */ public function getDeleteRequested() { return $this->deleteRequested; } }
我找到了我想要的东西.要从控制器访问上传到symfony的文件,您只需执行以下操作:
$request = $this->getRequest(); $file = $request->files->get('file1'); //file1 being the name of my form field for the file /* if your entity is set up like mine - like they teach you in the symfony2 cookbook * file is actually a public property so you can just set it like this **/ $entity->file = $file; //and here's how you get the original name of that file $entity->setName( $file->getClientOriginalName() ); (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |