Load file content inside controller in symfony 2

Symfony2 provides a component called "Finder", check the doc


I found a simple solution if you have the path to the file then

$file = new SplFileInfo('/path/to/file.css', '', '');

and the method getContents does the magic

$file->getContents();

But you will have to include the class

use Symfony\Component\Finder\SplFileInfo;

is also part of the finder library, but you don't need to search for the file you want to read.


I think you would first need to get the path to the CSS directory, using something like:

$path = $this->get('kernel')->getRootDir() . '/../css' . '/path/to/file.css';

Then, load the CSS file into a string:

$css = file_get_contents($path);

Hope that helps!

Tags:

Php

Symfony