我是Symfony的新人。我想创建一个可重复使用的表单,我可以上传图像。我已阅读文档。但每次上传图像时,它都会保存为.tmp文件而不是我给出的路径。我已经尝试了三天,不知道我做错了什么:(任何意见都非常感谢。:)
实体 `
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\validator\Constraints as Assert;
use Symfony\Component\HttpFoundation\File\File;
/**
* image
*
* @ORM\Table(name="media")
* @ORM\Entity(repositoryClass="test\ImageBundle\Entity\Image \imageRepository")
* @ORM\HasLifecycleCallbacks
*/
class image
{
/**
* @var integer
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @var string
*
* @ORM\Column(name="path",type="string",length= 255, nullable=true)
*/
public $path;
/**
* @var string
*
* @ORM\Column(name="alt",type="string",length= 255, nullable=true)
*/
private $ImageAlt;
/**
* Image file
*
* @var File
*
* @Assert\File(
* maxSize = "5M",
* mimeTypes = {"image/jpeg", "image/gif", "image/png", "image/tiff"},
* maxSizeMessage = "The maxmimum allowed file size is 5MB.",
* mimeTypesMessage = "Only the filetypes image are allowed."
* )
*/
protected $file;
/**
* @var string
*
* @ORM\Column(name="content", type="string", length=1024)
*/
private $content;
/**
* @var string
*
* @ORM\Column(name="url", type="string", length=128)
*/
private $url;
public function getUploadRootDir()
{
return __dir__.'/../../../../web/uploads';
}
public function getAbsolutePath()
{
return null === $this->path ? null : $this->getUploadRootDir().'/'.$this->path;
}
public function getAssetPath()
{
return 'uploads/'.$this->path;
}
/**
* @ORM\PrePersist()
* @ORM\PreUpdate()
*/
public function preUpload()
{
$this->tempFile = $this->getAbsolutePath();
$this->oldFile = $this->getPath();
if (null !== $this->file)
$this->path = sha1(uniqid(mt_rand(),true)).'.'.$this->file->guessExtension();
}
/**
* @ORM\PostPersist()
* @ORM\PostUpdate()
*/
public function upload()
{
if (null !== $this->file) {
$this->file->move($this->getUploadRootDir(),$this->path);
unset($this->file);
if ($this->oldFile != null) unlink($this->tempFile);
}
}
/**
* @ORM\PreRemove()
*/
public function preRemoveUpload()
{
$this->tempFile = $this->getAbsolutePath();
}
/**
* @ORM\PostRemove()
*/
public function removeUpload()
{
if (file_exists($this->tempFile)) unlink($this->tempFile);
}
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
public function getPath()
{
return $this->path;
}
/**
public function getName()
{
var_dump($this->name);
return $this->name;
}
*/
/**
* @return string
*/
public function getImageAlt()
{
return $this->ImageAlt;
}
/**
* @param string $ImageAlt
*/
public function setImageAlt($ImageAlt)
{
$this->ImageAlt = $ImageAlt;
}
/**
* Set content
*
* @param string $content
* @return image
*/
public function setContent($content)
{
$this->content = $content;
return $this;
}
/**
* Get content
*
* @return string
*/
public function getContent()
{
return $this->content;
}
/**
* Set url
*
* @param string $url
* @return image
*/
public function setUrl($url)
{
$this->url = $url;
return $this;
}
/**
* Get url
*
* @return string
*/
public function getUrl()
{
return $this->url;
}
} `
表格
<?php
namespace test\ImageBundle\Form\Image;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
class imageType extends AbstractType
{
/**
* @param FormBuilderInterface $builder
* @param array $options
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('path','file',array(
'label' => 'Image',
'required' => true
))
->add('ImageAlt')
->add('content')
->add('url')
;
}
/**
* @param OptionsResolverInterface $resolver
*/
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'test\ImageBundle\Entity\Image\image'
));
}
/**
* @return string
*/
public function getName()
{
return 'test_imagebundle_image_image';
}
}
控制器
<?php
namespace test\ImageBundle\Controller\Image;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
use test\ImageBundle\Entity\Image\image;
use test\ImageBundle\Form\Image\imageType;
/**
* Image\image controller.
*
* @Route("/image")
*/
class imageController extends Controller
{
/**
* Lists all Image\image entities.
*
* @Route("/", name="image")
* @Method("GET")
* @Template()
*/
public function indexAction()
{
$em = $this->getDoctrine()->getManager();
$entities = $em->getRepository('testImageBundle:Image\image')->findAll();
return array(
'entities' => $entities,
);
}
/**
* Creates a new Image\image entity.
*
* @Route("/", name="image_create")
* @Method("POST")
* @Template("testImageBundle:Image\image:new.html.twig")
*/
public function createAction(Request $request)
{
$entities= new image();
$form = $this->createCreateForm($entities);
$form->handleRequest($request);
if ($form->isValid()) {
$em = $this->getDoctrine()->getManager();
$em->persist($entities);
$em->flush();
return $this->redirect($this->get('router')->generate($request->get('_route')));
}
return array(
'entity' => $entities,
'form' => $form->createView(),
);
}
/**
* Creates a form to create a Image\image entity.
*
* @param image $entity The entity
*
* @return \Symfony\Component\Form\Form The form
*/
private function createCreateForm(image $entity)
{
$form = $this->createForm(new imageType(), $entity, array(
'action' => $this->generateUrl('image_create'),
'method' => 'POST',
));
$form->add('submit', 'submit', array('label' => 'Create'));
return $form;
}
/**
* Displays a form to create a new Image\image entity.
*
* @Route("/new", name="image_new")
* @Method("GET")
* @Template()
*/
public function newAction()
{
$entity = new image();
$form = $this->createCreateForm($entity);
return array(
'entity' => $entity,
'form' => $form->createView(),
);
}
/**
* Finds and displays a Image\image entity.
*
* @Route("/{id}", name="image_show")
* @Method("GET")
* @Template()
*/
public function showAction($id)
{
$em = $this->getDoctrine()->getManager();
$entity = $em->getRepository('testImageBundle:Image\image')->find($id);
if (!$entity) {
throw $this->createNotFoundException('Unable to find Image\image entity.');
}
$deleteForm = $this->createDeleteForm($id);
return array(
'entity' => $entity,
'delete_form' => $deleteForm->createView(),
);
}
/**
* Displays a form to edit an existing Image\image entity.
*
* @Route("/{id}/edit", name="image_edit")
* @Method("GET")
* @Template()
*/
public function editAction($id)
{
$em = $this->getDoctrine()->getManager();
$entity = $em->getRepository('testImageBundle:Image\image')->find($id);
if (!$entity) {
throw $this->createNotFoundException('Unable to find Image\image entity.');
}
$editForm = $this->createEditForm($entity);
$deleteForm = $this->createDeleteForm($id);
return array(
'entity' => $entity,
'edit_form' => $editForm->createView(),
'delete_form' => $deleteForm->createView(),
);
}
/**
* Creates a form to edit a Image\image entity.
*
* @param image $entity The entity
*
* @return \Symfony\Component\Form\Form The form
*/
private function createEditForm(image $entity)
{
$form = $this->createForm(new imageType(), $entity, array(
'action' => $this->generateUrl('image_update', array('id' => $entity->getId())),
'method' => 'PUT',
));
$form->add('submit', 'submit', array('label' => 'Update'));
return $form;
}
/**
* Edits an existing Image\image entity.
*
* @Route("/{id}", name="image_update")
* @Method("PUT")
* @Template("testImageBundle:Image\image:edit.html.twig")
*/
public function updateAction(Request $request, $id)
{
$em = $this->getDoctrine()->getManager();
$entity = $em->getRepository('testImageBundle:Image\image')->find($id);
if (!$entity) {
throw $this->createNotFoundException('Unable to find Image\image entity.');
}
$deleteForm = $this->createDeleteForm($id);
$editForm = $this->createEditForm($entity);
$editForm->handleRequest($request);
if ($editForm->isValid()) {
$em->flush();
return $this->redirect($this->generateUrl('image_edit', array('id' => $id)));
}
return array(
'entity' => $entity,
'edit_form' => $editForm->createView(),
'delete_form' => $deleteForm->createView(),
);
}
/**
* Deletes a Image\image entity.
*
* @Route("/{id}", name="image_delete")
* @Method("DELETE")
*/
public function deleteAction(Request $request, $id)
{
$form = $this->createDeleteForm($id);
$form->handleRequest($request);
if ($form->isValid()) {
$em = $this->getDoctrine()->getManager();
$entity = $em->getRepository('testImageBundle:Image\image')->find($id);
if (!$entity) {
throw $this->createNotFoundException('Unable to find Image\image entity.');
}
$em->remove($entity);
$em->flush();
}
return $this->redirect($this->generateUrl('image'));
}
/**
* Creates a form to delete a Image\image entity by id.
*
* @param mixed $id The entity id
*
* @return \Symfony\Component\Form\Form The form
*/
private function createDeleteForm($id)
{
return $this->createFormBuilder()
->setAction($this->generateUrl('image_delete', array('id' => $id)))
->setMethod('DELETE')
->add('submit', 'submit', array('label' => 'Delete'))
->getForm()
;
}
}
答案 0 :(得分:0)
getUploadRootDir()
- 你确定它是正确的路径和文件夹是否存在适当的权限?看看https://github.com/dustin10/VichUploaderBundle - 它可以保存您接下来的三天。