Monday, March 20, 2017

Laravel - Create file with specific user, group and permissions

I try to create a file by using put and give the file the permission 777.

If I create the File like this:

use File;

...

$content = "<?php echo 'test'; ?>";
$file   = app_path() . '/Http/Controllers/test.php';
File::put($file, $content);

However, the file is created with this rights:

-rw-r--r-- 1 daemon daemon 2,2K Mär 14 08:08 test.php

Also the user and group is daemon instead of root.

How can I create a file with user and group root and with permissions rwxrwxrwx ?

e.g.

-rwxrwxrwx 1 root root 2,2K Mär 14 08:08 test.php


I also added these lines to my /etc/sudoers

www-data ALL=(ALL) NOPASSWD: /bin/chmod 
www-data ALL=(ALL) NOPASSWD: /bin/chown
www-data ALL=(ALL) NOPASSWD: /bin/chgrp

But I still get chmod(): no permission with the following code:

File::chmod($unitPath, 0777);
chown($unitPath,"root");
chgrp($unitPath,"root");



via Black

Advertisement