2011-12-08 5 views
0

hello guys가 파일 업 로더를 만들기 위해 문서를 따르지 만, 왜 내가 파일을 업로딩 할 때 오류가 없음을 알 수 있습니까? 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__ 직후에 '/'를 잊어 버렸습니다. 이제는 작동합니다. 이것을 시도하십시오 : __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