2012-08-04 10 views
1

& 온라인 리소스에서 PDF 파일을 다운로드하려고합니다. 필자는 파일 업로드에 대한 요리 책 장을보고 나 자신의 기술을 수정하려고했습니다. 나는 단지 이것을 작동시킬 수 없다.Symfony2를 사용하여 PDF 파일 다운로드 및 저장

컨트롤러 코드 :

namespace Acme\DemoBundle\Controller; 

use Symfony\Bundle\FrameworkBundle\Controller\Controller; 
use Symfony\Component\HttpFoundation\Request; 
use Symfony\Component\HttpFoundation\Response; 

use Acme\DemoBundle\Entity\Shipment; 

class WelcomeController extends Controller 
{ 
    public function indexAction(Request $request) 
    { 
     echo $barcodeUrl = "http://www.website.com/somefile.pdf"; 

     $Shipment = new Shipment(); 
     $Shipment->setBarcodeUrl($barcodeUrl); 

     $em = $this->getDoctrine()->getEntityManager(); 
     $em->persist($Shipment); 
     $em->flush(); 

     // Saving The File: 
     $saveto = __DIR__.'/../../../../web/uploads/documents'; 
     $ch = curl_init($barcodeUrl); 
     $fp = fopen($saveto,'wb'); 
     curl_setopt($ch, CURLOPT_FILE, $fp); 
     curl_setopt($ch, CURLOPT_HEADER, 0); 
     curl_exec($ch); 
     curl_close($ch); 
     fclose($fp); 

     return new Response('Created shipment and saved file"); 
    } 

    protected function getUploadRootDir() 
    { 
     return __DIR__.'/../../../../web'.$this->getUploadDir(); 
    } 

    protected function getUploadDir() 
    { 
     return 'uploads/documents'; 
    } 
} 

때마다 나는 위의 코드를 실행합니다. 그것은 웹 폴더에 파일을 저장합니다.이 파일은 이름으로 언급 한 경로가 있습니다. 파일이 열리지 않고 확장자가 저장되지 않습니다.

저장된 파일 : 웹 폴더에 /../../../../web/home/webmuch/uploads.

답변

2

실제로 Symfony2 문제는 아닙니다. fopen하기 위해 파일 이름을 전달해야합니다. 이 같은 그래서해야 뭔가 :

$fp = fopen(sprintf('%s/%s.%s', $saveto, sha1($barcodeUrl), 'pdf'),'wb'); 

은 또한 당신이 당신이 당신을 위해 작동하지 않았기 때문에, 나는 믿고있어 사용하지 않을 getUploadRootDir() 방법이 발견은 - 그 이유는 당신이 놓치고있는 것입니다 그래서 $this->container->getParameter('kernel.root_dir').'/../web'

와이

protected function getUploadRootDir() 
{ 
    return __DIR__.'/../../../../web/'.$this->getUploadDir(); 
} 

마지막으로, 당신이 얻을 수있는 웹 위치에 슬래시

+0

안녕하세요, 답변을 주셔서 감사합니다. $ this-> container-> get ('kernel.root_dir') 오류가 발생했습니다. 존재하지 않는 서비스 "kernel.root_dir"을 요청했습니다. – Aayush

+0

그리고 이전의 방법은 슬래시에도 작동하지 않습니다. – Aayush

+0

죄송합니다. $ this-> container-> getParameter ('kernel.root_dir') – Inoryy

관련 문제