How can the PHP function chown() be used to change both permissions and ownership of files and folders, and what are the limitations in doing so?
To change both permissions and ownership of files and folders using the PHP function chown(), you can specify the desired user and group ownership along with the file or directory path. However, it's important to note that this function may have limitations based on the server configuration and the permissions of the PHP process. Make sure to test the functionality on a development environment before implementing it in a production setting.
$file_path = '/path/to/file.txt';
$user = 'new_user';
$group = 'new_group';
if (chown($file_path, $user) && chgrp($file_path, $group)) {
echo "Ownership and permissions changed successfully.";
} else {
echo "Failed to change ownership and permissions.";
}