2

I want to creat file this path : /var/logs/filename.fileextension . But ı dont create file ı have error ->Failed to touch "/var/logs/file.txt".

My Codes:

$fs=new Filesystem();
$fs->touch('/var/logs/file.txt');
Burhan Yılmaz
  • 774
  • 8
  • 21

1 Answers1

10

touch() sets access and modification time for a file. The current time is used by default. You can set your own with the second argument. The third argument is the access time:

So touch doesnt create a new file according to the docs.

read http://symfony.com/doc/current/components/filesystem.html

if you want to create a file you need to use the function dumpFile();

dumpFile() allows you to dump contents to a file. It does this in an atomic manner: it writes a temporary file first and then moves it to the new file location when it's finished. This means that the user will always see either the complete old file or complete new file (but never a partially-written file):

read http://symfony.com/doc/current/components/filesystem.html

This code should create a new empty file.

$fs=new Filesystem();
$fs->dumpFile('/var/logs/file.txt', '');
Raymond Nijland
  • 11,488
  • 2
  • 22
  • 34
  • 1
    thank u Raymond its work :) $fs->dumpFile($this->cont->get('kernel')->getRootDir().'/../var/logs/file.txt','content'); – Burhan Yılmaz May 16 '17 at 11:36
  • Symfony Filesystem [touch method](https://github.com/symfony/filesystem/blob/3.4/Filesystem.php) (in 3.4 at least) uses the php function touch() which does create a file according to [php documentation](http://php.net/manual/en/function.touch.php). Usually it fails because of permission issues. It also happened to me because I tried creating a file inside a non-existing directory. – Mosset Jérémie Jul 04 '18 at 13:31