你好跟随文档制作一个文件上传器,但我无法找到为什么它不工作当我点击提交它给出错误Unable to create the "uploads/cv" directory
虽然这些文件夹已存在于project / src / My / UserBundle / Resources /公共/图像
我的文档实体
namespace My\UserBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
/**
* My\UserBundle\Entity\Document
*
* @ORM\Table()
* @ORM\Entity(repositoryClass="My\UserBundle\Entity\DocumentRepository")
*/
class Document
{
/**
* @var integer $id
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @var string $path
*
* @ORM\Column(name= "name",type="string", length=255)
*/
private $name;
/**
* @var string $path
*
* @ORM\Column(type="string", length=255, nullable=true)
*/
private $path;
/**
* @Assert\File(maxSize="6000000")
*/
private $file ;
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set path
*
* @param string $path
*/
public function setPath($path)
{
$this->path = $path;
}
/**
* Get path
*
* @return string
*/
public function getPath()
{
return $this->path;
}
function getFile()
{
return $this->file;
}
function setFile($file)
{
$this->file =$file;
}
function preUpload()
{
if(null !== $this->file )
{
$this->path = uniqid() . '.' . $this->file->guessExtension();
}
}
function upload()
{
if(null === $this->file)
{
return ;
}
$this->file->move( $this->getUploadDir() ,$this->getPath );
unset($this->file);
}
function removeUpload()
{
if($file = $this->getAbsolutePath() )
{
unlink($file);
}
}
function getWebPath()
{
return $this->getUploadDir() . $this->getPath() ;
}
function getUploadDir()
{
return 'uploads/cv' ;
}
function getAbsolutePath()
{
return $this->getRootDir() . $this->getPath() ;
}
function getRootDir()
{
return __DIR__ . '../../../../src/' .$this->getUploadDir() ;
}
function setName($n)
{
$this->name =$n ;
}
function getName()
{
return $this->name ;
}
}
和我的控制器动作
function uploadAction()
{
$document = new Document();
$form = $this->createFormBuilder($document)
->add('name')
->add('file')
->getForm();
$request = $this->getRequest() ;
if( $request->getMethod() == 'POST' )
{
$form->bindRequest($request);
if($form->isValid() )
{
$em = $this->getDoctrine()->getEntityManager() ;
$document->upload();
$em->persist($document);
$em->flush();
return
$this->render('MyUserBundle:Document:upload.html.twig');
}
}
else
{
return
$this->render('MyUserBundle:Document:upload.html.twig' ,array('form'=>$form->createView() );
}
提前致谢
答案 0 :(得分:1)
我认为您在错误的路径中创建了uploads/cv
文件夹。
应该是project/web/uploads/cv
,而不是project/src/My/UserBundle/Resources/public/images
。
然后使用chmod
向Symfony授予对该文件夹的写入权限。