简体   繁体   中英

Symfony: get file in controller from web folder

I want to get pdf file from web folder in controller, but can't figure out how to do it.

I've try to get path like this

// 1
$file = __DIR__ . '/../../../../web/1.pdf';  

// 2
$file = $this->get('kernel')->getRootDir().'\..\web\1.pdf';

// 3
$isSecure = $request->isSecure() ? 'https://' : 'http://';
$file = $isSecure . $request->getHost() . $this->container->get('assets.packages')->getUrl('1.pdf');

and then use file controller helper but it didn't work.

How can I get that file from public directory?

Try this -

Append this to autoload.php

define('APPLICATION_PATH', realpath(__DIR__) . '/');
define('WEB_PATH',  APPLICATION_PATH . '../web/');

also you can add another folders

Then

file_get_content(WEB_PATH . 'your_file.pdf');

Another way -

$path = $this->getParameter('dir.downloads');

But don't forget to declare it on parameters.yml like this

parameters:
    ....
    dir.downloads: var/www (your folder)
    ....

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM