How to create a file from a controller

I am trying to save a file from a controller. It doesn't work with help fopen

and I can't get the error message. Is there a way to do this, or is it absolutely necessary to use a filesystem package? And is there a way to understand the error? I've used try / catch blog with no luck. The code I'm using is very simple:

$file = $this->get('request')->server->get('DOCUMENT_ROOT').'/myfile.txt';
$fp = fopen($file, 'w');
fwrite($fp, 'file content');
fclose($fp);

      

+3


source to share


1 answer


There are many ways to do this. I use very often __DIR__

to get the actual folder.

$fp = fopen(__DIR__.'/myfile.txt');

      



It's much easier if the file is in the same folder. If you have an older version of PHP you should use dirname(__FILE__)

. Or in Symfony2 you can use some functions to get the directory. Something like that:

$path = $this->get('kernel')->getRootDir() . '/../web';

      

0


source







All Articles