简体   繁体   English

Laravel-使用特定的用户,组和权限创建文件

[英]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 . 我尝试通过使用put创建文件, put文件授予权限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 . 用户和组也是daemon而不是root

How can I create a file with user and group root and with permissions rwxrwxrwx ? 如何创建具有用户和组root以及权限rwxrwxrwx

eg 例如

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

I also added these lines to my /etc/sudoers 我还将这些行添加到我的/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: 但是我仍然得到chmod(): no permission以下代码chmod(): no permission

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

Laravel has built-in support for changing mod Laravel具有更改模块的内置支持

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 不幸的是,它的Filesystem模块中没有chown,但是您可以将php称为

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. 请注意:即使您不是超级用户,更改组和所有者也会失败,即使您可以sudo。

I solved it by using shell_exec() 我通过使用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 . 通过在文件/etc/sudoers输入以下行,确保允许www-data执行命令。

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. 始终使用visudo来编辑/etc/sudoers否则您很容易出现语法错误并损坏文件并失去使用sudo的可能性,并且您需要通过从实时CD引导来修复系统。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM