& 온라인 리소스에서 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
.
안녕하세요, 답변을 주셔서 감사합니다. $ this-> container-> get ('kernel.root_dir') 오류가 발생했습니다. 존재하지 않는 서비스 "kernel.root_dir"을 요청했습니다. – Aayush
그리고 이전의 방법은 슬래시에도 작동하지 않습니다. – Aayush
죄송합니다. $ this-> container-> getParameter ('kernel.root_dir') – Inoryy