I'm having problems when trying to write a file with a different user rather than 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'
I'm using Symfony 5 Filesystem to move files around but i still use PHP's fopen to create those files. 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).
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'.
Is there any way i can change the file owner with sudo?
$filesystem->chown($url_origin, 'myself', false);
This returns an error -> Failed to chown file "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:
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".
The way is to set "chmod" or change configuration in your server with php-fpm running with myself user.
Regards