LonghornPHP 2026

chown

(PHP 4, PHP 5, PHP 7, PHP 8)

chownCambia el propietario del fichero

Descripción

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

Cambia el propietario del fichero filename a user. Solo el superusuario (root) puede cambiar arbitrariamente el propietario de un fichero.

Parámetros

filename

Ruta hacia el fichero.

user

Un nombre o un número de usuario.

Valores devueltos

Esta función retorna true en caso de éxito o false si ocurre un error.

Ejemplos

Ejemplo #1 Ejemplo con chown()

<?php
// Nombre del fichero y nombre de usuario a utilizar
$file_name= "foo.php";
$path = "/home/sites/php.net/public_html/sandbox/" . $file_name ;
$user_name = "root";
// Define el usuario
chown($path, $user_name);
// Verificación del resultado
$stat = stat($path);
print_r(posix_getpwuid($stat['uid']));
?>

Resultado del ejemplo anterior es similar a:

Array
(
 [name] => root
 [passwd] => x
 [uid] => 0
 [gid] => 0
 [gecos] => root
 [dir] => /root
 [shell] => /bin/bash
)

Notas

Nota:

Esta función no funciona con los archivos remotos, ya que el archivo examinado debe ser accesible en el sistema de archivos del servidor.

Nota:

En Windows, esta función falla silenciosamente cuando se aplica sobre un fichero ordinario.

Véase también

  • chmod() - Cambia el modo del fichero
  • chgrp() - Cambia el grupo de un fichero

Found A Problem?

Learn How To Improve This PageSubmit a Pull RequestReport a Bug
+add a note

User Contributed Notes 2 notes

up
7
martijn at sigterm dot nl
23 years ago
If chown is filled with a variable ( chown ("myfile", $uid) the uid will be looked up through pwget_uid.
So if you need to set a non existing uid use inval($uid).
up
3
mindlessconsumer+phpnet at gmail dot com
1 year ago
It may be worth making explicitly clear that, while the shell's `chown` command allows both user and group to be set in one system call like this `chown username:groupname filename`, PHP's version unfortunately does not: 
<?php
// This will not work. 
chown($filename, 'username:groupname');
// You have to use two separate calls.
chown($filename, 'username');
chgrp($filename, 'groupname');
?>
+add a note

AltStyle によって変換されたページ (->オリジナル) /