简体   繁体   English

PHP 在 fopen 上更改默认用户

[英]PHP Change default User on fopen

I'm having problems when trying to write a file with a different user rather than www-data.尝试使用不同的用户而不是 www-data 编写文件时遇到问题。 I need to leave the file on a mapped unit that it's NOT mine so i cannot change permissions on that one.我需要将文件保留在它不是我的映射单元上,因此我无法更改该单元的权限。 Insetead i have a username and a group, let's call them 'myself':'myself' Insetead 我有一个用户名和一个组,让我们称它们为“我自己”:“我自己”

I'm using Symfony 5 Filesystem to move files around but i still use PHP's fopen to create those files.我正在使用 Symfony 5 文件系统来移动文件,但我仍然使用 PHP 的 fopen 来创建这些文件。 This is what i have so far.这是我到目前为止。

private function moveAndDeleteFile($url_origin, $url_destiny)
{
    $filesystem = new Filesystem();

    $a = fopen($url_origin, 'wa+');
    fwrite($a, 'Test');
    fclose($a);

    $filesystem->copy($url_origin, $url_destino, true);

    if (!$filesystem->exists($url_destiny)) {
        return false;
    }
    $filesystem->remove($url_origin);

    return true;
}

This is just a test, so i'm creating a file (name included in $url_origin) and try to copy the file to $url_destiny and then remove the original (again, just a test).这只是一个测试,所以我正在创建一个文件(名称包含在 $url_origin 中)并尝试将该文件复制到 $url_destiny,然后删除原始文件(同样,只是一个测试)。

The ting is that the file is always created by www-data and for my target directory i need to set the file owner to 'myself'.问题是该文件始终由 www-data 创建,对于我的目标目录,我需要将文件所有者设置为“我自己”。

Is there any way i can change the file owner with sudo?有什么办法可以用 sudo 更改文件所有者吗?

$filesystem->chown($url_origin, 'myself', false);

This returns an error -> Failed to chown file "my/route/file.xml"这将返回错误 -> 无法 chown 文件“my/route/file.xml”

Same thing happens when trying尝试时也会发生同样的事情

$filesystem->chgrp($url_origin, 'myself', false);

Then i tried to use the default PHP chown() function like this:然后我尝试使用默认的 PHP chown() 函数,如下所示:

chown($url_origin, 'myself');

And get this other Error: Operation not permitted.并得到另一个错误:不允许操作。

I guess that i need to specify somewhere the user's properties but i'm clueless right now.我想我需要在某个地方指定用户的属性,但我现在一无所知。 Any ideas on how to pass this through?关于如何通过这个的任何想法? i'm sure i'm missing something obvious.我确定我错过了一些明显的东西。

Thanks-.谢谢-。

You cannot use "chown" because only "root" can use "chown".您不能使用“chown”,因为只有“root”才能使用“chown”。

The way is to set "chmod" or change configuration in your server with php-fpm running with myself user.方法是设置“chmod”或使用自己用户运行的php-fpm更改服务器中的配置。

Regards问候

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

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