symfonytwigsymfony-2.1

Symfony2 : access files in folder outside web


I have a page in symfony2 with a java applet and javascript functions that make the java applet load files. These files are user-dependant and each file has to be stored in the user folder.

Users have their folder in a directory located at the root of the symfony folder (/temp/). Now i need the files in these folders to be available (like an asset?).

// Controler ( What i have now )
$filepath = $this->get('kernel')->getRootDir().'/../temp/'.$userfolder;

// View ( what i would like -using twig )
{% set path = userfolder ~ '/' ~ filename %}
<a href="javascript:document.jmol.script('load {{ asset(path) }}; frame all');">link</a>

Solution

  • If your doc root is at the web directory, you cannot do what you are trying to do. This is the "root" directory which means you cannot access anything outisde it from the browser. This is a good thing, otherwise people browsing your site would be able to access other directories like your app security configuration, etc...

    You have several options:

    a) use a subdirectory of web to hold your user files, for example web/temp

    b) hold them somewhere else and copy them to a web subfolder as needed. This is what symfony does for assets

    c) create a new controller which intercepts routes like 'temp/{user}/{filename}' and then serves these files from the directory where you hold the files.