phpfile-ownership

Changing Owner/Group ID In PHP


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));
      ?>