Symfony 2:文件上传时出现了一个奇怪的错误

Symfony 2:文件上传时出现了一个奇怪的错误,symfony,file-upload,Symfony,File Upload,大家好,我在文档后面做了一个文件上传程序,但是我不知道为什么当我点击submit时它不起作用,它给出了一个错误无法创建“uploads/cv”目录,尽管这些文件夹已经存在于project/src/My/UserBundle/Resources/public/images中 我的文档实体 namespace My\UserBundle\Entity; use Doctrine\ORM\Mapping as ORM; use Symfony\Component\Validator\Constra

大家好,我在文档后面做了一个文件上传程序,但是我不知道为什么当我点击submit时它不起作用,它给出了一个错误
无法创建“uploads/cv”目录
,尽管这些文件夹已经存在于project/src/My/UserBundle/Resources/public/images中

我的文档实体

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() );
            } 

提前感谢

我认为您在错误的路径中创建了
上传/cv
文件夹

它应该是
project/web/uploads/cv
,而不是
project/src/My/UserBundle/Resources/public/images


然后使用
chmod

向Symfony授予该文件夹的写入权限。可能我错了,但我不确定您是否可以在该文件夹中写入,因为您在该文件夹中没有写入权限。也许其他用户可以比我帮你更多。我只是有同样的问题,我忘记了一个“/”,就在DIR之后。现在它工作了。试试这个:return DIR.'/../../../../..//src/'.$this->getUploadDir();