Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

php

What is the chown() function in PHP?

Muhammad Ashir

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

chown() is a built-in function in PHP that changes the owner or user group of a file. Only the superuser has the privileges to change the ownership of a file.

Syntax

chown(string $filename, string|int $user): bool

Parameters

  • filename: The name of the file as a string.
  • user: The name of the user as either a string or an integer.

Return value

chown() returns true on success and false on failure.

Code

main.php
temp
<?php
$file_name= "temp";
$path = "./" . $file_name ;
$user_name = "root";
chown($path, $user_name);
$stat = stat($path);
$usr = strval($stat['uid']);
print_r("UID: ".$usr."(root)\n");
print_r(posix_getpwuid($stat['uid']));
?>

The code above shows how the chown() function is used. We provide the user_name and path to chown() and it changes the owner to user_name. The stat() function provides information about a file, which is the temp file in this case. Lastly, we print the UID, which is 0. This denotes that the UID of the file’s owner is indeed root.

RELATED TAGS

php

CONTRIBUTOR

Muhammad Ashir
Copyright ©2022 Educative, Inc. All rights reserved

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring