2011-12-08 44 views
0

你好人跟隨文件,使文件上傳,但我無法找出爲什麼它不工作,當我點擊提交它給出了一個錯誤Unable to create the "uploads/cv" directory雖然這些文件夾已經存在項目/ src /我/ UserBundle /資源/公/圖像Symfony 2:一個奇怪的錯誤與文件上傳

我的文檔實體

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

我只是有同樣的問題,我忘了__DIR__之後的'/'。現在它可以工作。試試這個:return __DIR__。 '/../../../../src/'。$ this-> getUploadDir(); –

回答

1

我認爲你創建了你的uploads/cv文件夾在錯誤的路徑。

它應該是project/web/uploads/cv而不是project/src/My/UserBundle/Resources/public/images

然後讓Symfony對該文件夾的寫入權限爲chmod

+0

noo其他我想存儲它的人 – kosaidpo

+0

可能是我錯了,但我不確定你可以寫在那個文件夾中,只是因爲你沒有寫權限。也許其他一些用戶可以幫助你比我更多。 – Andrea

+0

好的,謝謝我生病了試試看;] – kosaidpo