Search code examples
phpsymfonydirectorytwigreaddir

Twig Code for a List of all files which are in {{file_location}} Symfony2


So far I have managed to show "the last file" in my twig view, and all of them over the controller which appears on my page at first line before the header. Now, I need to display all in the twig view.

This is my code:

controller

public function frmyfilesAction(Request $request)    { $session = $this->getRequest()->getSession(); $em = $this->getDoctrine()->getEntityManager(); $repository = $em->getRepository('redlabLabelBundle:Users');
    if ($session->has('login')) 
    {
        $login = $session->get('login');
        $username = $login->getUsername();
        $password = $login->getPassword();
        $user = $repository->findOneBy(array('userName' => $username, 'password' => $password));
        $dir = $user->getServer();

        if ($dh = opendir($dir))
        {
            $store_array = array() ;
            while (($file = readdir($dh)) !== false)    
            {
                echo $file . "</br>";
                $store_array = $file;
            }
            $mefil = $store_array;
                                }
            closedir($dh);

            return $this->render('redlabLabelBundle:Advert:frmyfiles.html.twig', array('mifil'=> $mefil,'name' => $user->getFirstName(),'fhost' => $user->getServer());
        }
        return $this->render('redlabLabelBundle:Advert:radiofr.html.twig'); }

twig view

 {%block body %} <div class="container">
     <h2>Ici sont stoqués tous tes fichiers {{name}}</h2></br></br>     <h4>Mon Dossier {{fhost}}<h4>   </br>

     <a href="{{fhost}}{{mifil}}">{{mifil}} </a>    </br></br></br>     </br></br></br>

     </div>  <a href="{{path('redlab_platform_logout')}}"
     >Déconnection</a></br></br></br></br>
 {%endblock%}

--

How do I bring ALL the data from the while loop in the array, so that I can use it in the twig view?


Solution

  • Twig is designed to render a view there is no Twig native function to read file names. You should get the file names in your crontroller (or even better in a service) and then send the data to your view. And if you really want to use Twig to do so, yes you will have to write a Twig extension but it's a very very very bad idea...