2012-12-20 4 views
0

사용자 특정 폴더에 파일을 업로드하는 컨트롤러를 만들고 싶습니다. 나는 현재 사용자가 컨트롤러에 포스트 데이터를 보내는 파일을 업로드 할 수있는 from을 가지고있다.파일 업로드를 처리하는 컨트롤러 만들기

컨트롤러에서 수행하고자하는 작업은 업로드 된 파일을 가져 와서 폴더에 넣는 것입니다. /public/{username}/files

하지만 심포니를 사용하여 어떻게 접근해야하는지 잘 모르겠습니다.

+0

요리 책 - 항목 "Doctrine으로 파일 업로드를 처리하는 방법"을 살펴보십시오. http://symfony.com/doc/current/cookbook/doctrine/file_uploads.html – dbrumann

+0

아 havent, 나는 생각하지 않았습니다. Doctrine에 관해서는,이 하하에게 여전히 정말로 새로운 것입니다. 그 덕분입니다. – ChaoticLoki

답변

1

의 Symfony2 워드 프로세서 여기에 유용한 도움이 될 수 있습니다.

추가 사항이있는 follow them입니다. "실제로 같은 별도의 엔티티 방법을 사용할 필요가 그것에게 당신은 늘 이런 식으로 저장

public function upload($username) 
{ 
    if (null === $this->file) { 
     return; 
    } 
    //use the username for the route 
    $this->file->move(
     "/public/$username/files/", 
     $this->file->getClientOriginalName() 
    ); 

    // set the path property to the filename where you've saved the file 
    $this->path = $this->file->getClientOriginalName(); 

    // clean up the file property as you won't need it anymore 
    $this->file = null; 
} 

:

if ($form->isValid()) { 
    $em = $this->getDoctrine()->getManager(); 
    //get the user and pass the username to the upload method 
    $user = $this->get('security.context')->getToken()->getUser(); 
    $document->upload($user->getUsername()); 

    $em->persist($document); 
    $em->flush(); 

    $this->redirect(...); 
} 

파일을 업로드

은, 사용자 이름을 사용 문서를 저장하면, 사용자 이름을 통과 getAbsolutePath "등 당신이 등 공간에 동의하면 사용자 이름을 slugify 할 수 있습니다

편집 : 당신은 설정해야합니다 나중에 파일을 찾을 수 있도록 파일에 대한 사용자의 oneToMany 관계.

+0

아, Doctrine을 사용하여 더 간단하게 만들 수 있다는 생각을하지 못했습니다. 감사합니다. – ChaoticLoki

0

이 Mahok 댓글을 달았대로 ---

$upload_dir = "your upload directory/{username}";   
if (!is_dir($upload_dir)) { 
    @mkdir($upload_dir, "755", true); 
} 
move_uploaded_file($source,$destination); 
관련 문제