chown

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

chownファイルの所有者を変更する

説明

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

ファイルfilenameの所有者を(名前または番号で指定した) ユーザーuserに変更しようと試みます。 スーパーユーザーのみがファイルの所有者を変更できます。

パラメータ

filename

ファイルへのパス。

user

ユーザー名あるいはユーザー番号。

戻り値

成功した場合に true を、失敗した場合に false を返します。

例1 シンプルな chown() の例

<?php

// 使用するファイル名とユーザー名
$file_name= "foo.php";
$path = "/home/sites/php.net/public_html/sandbox/" . $file_name ;
$user_name = "root";

// ユーザーを設定します
chown($path, $user_name);

// 結果を確認します
$stat = stat($path);
print_r(posix_getpwuid($stat['uid']));

?>

上の例の出力は、 たとえば以下のようになります。

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

注意

注意: この関数では、 リモートファイル を 使用することはできません。これは、処理されるファイルがサーバーの ファイルシステムによりアクセスできる必要があるためです。

注意: Windows で通常ファイルに対してこの関数を実行すると、静かに失敗します。

参考

  • chmod() - ファイルのモードを変更する
  • chgrp() - ファイルのグループを変更する

add a note

User Contributed Notes 2 notes

up
7
martijn at sigterm dot nl
21 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
0
mindlessconsumer+phpnet at gmail dot com
24 days 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');
?>
To Top