Changing Owner/Group ID In PHP
-
09-12-2019 - |
Question
In my client's ftp the old files have 3002 3000
value in owner/group column.
But when I upload a new page it has 3002 3002 and I can't access this file.
When I try to load this new page it displays error:
NetworkError: 500 Internal Server Error
Why this happen?
If there is any php code to change the 3002 3002
to 3002 3000
?
How can I change owner/group value ? Can I change the owner/group Id using any php code?
I already used chown()
function , but nothing will happen the owner/group column value in ftp still displaying 3002 3002
Solution
<?php
// File name and username to use
$file_name= "test.php";
$path = "/home/sites/public_html/login/" . $file_name ;
$user_name = "root";
// Set the user
chown($path, $user_name);
// Check the result
$stat = stat($path);
print_r(posix_getpwuid($stat['uid']));
?>
It will return something like :-
Array
(
[name] => root
[passwd] => x
[uid] => 0
[gid] => 0
[gecos] => root
[dir] => /root
[shell] => /bin/bash
)
For Changing a file's group :-
<?php
$filename = 'file.txt';
$format = "%s's Group ID @ %s: %d\n";
printf($format, $filename, date('r'), filegroup($filename));
chgrp($filename, 8);
clearstatcache(); // do not cache filegroup() results
printf($format, $filename, date('r'), filegroup($filename));
?>
OTHER TIPS
Use chown()
to change the files' owner.
When i try to load this new page it displays error:
And why are you trying to change the owner and group of the file? Is it at all possible that the error is actually in something else? Try checking the logs to see what the error "500" means. If you've moved servers, the owner and group may not be the only issue, it may be that some PHP modules haven't been compiled, while you're relying on them.
A permissions issue would actually probably yield a 403 Forbidden, not a 500 Internal Server Error. Again, check your logs to find out what is actually going wrong.