chown(string $filename, string|int $user): The bool function is used to change the owner of a specified file or directory. It is usually used in scenarios where file permissions need to be dynamically adjusted, such as modifying its ownership after uploading a file.
Sample code:
<?php
$file = '/path/to/file.txt';
$user = 'www-data'; // New owner username
if (chown($file, $user)) {
echo "File owner has been changed to {$user}";
} else {
echo "Change file owner failed";
}
?>
Insufficient permissions
The chown() function requires sufficient permissions, and generally only the superuser (root) can change the file owner. If the user currently running the PHP script is not root, the system will deny permissions, resulting in this error.
File system limitations
Some file systems (such as NFS, FAT32, ext4 of some mount options) do not support changing the file owner, and an error will also be reported when calling chown() .
Security module limitations
Security modules (such as SELinux, AppArmor, etc.) may be enabled on the server, which may limit changes to the file owner.
PHP configuration restrictions
Some PHP operating environments or security policies (such as open_basedir restrictions) may indirectly lead to insufficient permissions.
Ensure that users in the PHP running environment have permission to execute chown() , which usually requires root permission. For production environments, the following methods can be used:
Use sudo in CLI mode
sudo -u root php script.php
Execute as root using system schedule task (cron)
Adjust the running user of PHP-FPM or web server if necessary
If you cannot use chown() , you can consider the following alternative:
Use chmod() to adjust permissions instead of owner:
chmod($file, 0644);
Execute through external shell commands (provided that permissions are available)
exec("sudo chown www-data " . escapeshellarg($file));
Note: When using exec() , be sure to ensure safety to prevent command injection.
Confirm whether the partition where the file is located supports chown() . If permissions are restricted during mount, consider remounting:
mount -o remount,defaults /mount/point
Or use a file system that supports permission operations.
If the server has SELinux or AppArmor enabled, the rules can be temporarily shut down or adjusted:
Close SELinux:
setenforce 0
Adjust relevant security policies to allow chown operations
chown(): Operation not permitted error is usually caused by insufficient permissions. The user where the PHP script is located must have sufficient permissions to change the file owner. Generally, PHP processes will not be run directly with root in production environments. It is recommended to perform operations with sudo through scheduling tasks or external commands.
At the same time, you need to pay attention to the permission restrictions of file system types and security modules, and choose appropriate solutions based on actual conditions.
<?php
$file = '/path/to/file.txt';
$user = 'www-data';
if (!file_exists($file)) {
die("The file does not exist");
}
if (@chown($file, $user)) {
echo "The file owner was successfully modified {$user}";
} else {
$error = error_get_last();
echo "Modification of owner failed,error message: " . ($error['message'] ?? 'Unknown error');
}
?>