2

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");
3
  • 1
    You will have to exec linux command with shell_exec for example. Commented Mar 14, 2017 at 15:25
  • why do you use File::put and not fopen or file_put_content Commented Mar 14, 2017 at 15:26
  • @AnnSophieAngermüller, because it is faster to code Commented Mar 14, 2017 at 15:30

2 Answers 2

5
+50

Laravel has built-in support for changing mod

ex:

File::chmod($file,0755); //the leading zero is a must

Unfortunately it doesn't has chown in its Filesystem module, but you can call the php one

chown($file,"root");

to change the group

chgrp($file,"group");

Note that : changing group and owner will fail if you're not a super user, even if you can sudo.

Sign up to request clarification or add additional context in comments.

1 Comment

I tried it and I always get chmod() no permission I also added this 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
1

I solved it by using shell_exec()

shell_exec('sudo chmod 0777 '. $unitPath);
shell_exec('sudo chown root:root '. $unitPath);

Make sure that www-data is allowed to execute the commands by entering these lines in the file /etc/sudoers.

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

Important! Always use visudo to edit /etc/sudoers otherwise you can easily make syntax errors and corrupt the file and loose the possibilty to use sudo, and you need to fix your system by booting from a live cd.

1 Comment

giving sudo access to www-data is great security risk.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.