PHPのお勉強!

PHP TOP

chmod

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

chmodファイルのモードを変更する

説明

chmod(string $filename, int $permissions): bool

指定されたファイルのモードを permissions で指定したものに変更しようと試みます。

パラメータ

filename

ファイルへのパス。

permissions

permissions は自動的には 8 進数と見なされないので注意してください。 意図した操作を行うには、permissions の前にゼロ(0)を付ける必要があります。 "g+w" のような文字列は正常に動作しません。

<?php
chmod
("/somedir/somefile", 755); // 10 進数; おそらく間違い
chmod("/somedir/somefile", "u+rwx,go+rx"); // 文字列; 正しくない
chmod("/somedir/somefile", 0755); // 8 進数; 正しいモードの値
?>

permissions 引数は 3 つの 8 進法による数値で構成され、 所有者自身、所有者が属するグループ、その他のユーザーの順で アクセス制限を設定します。一つ一つの数字はそのターゲットに対し 許可を与えます。1 は実行権限、2 はファイルに対する書き込み権限、 4 はファイルに対する読み込み権限を与えます。 必要な権限にあわせ数値を加算してください。 許可モードに関する詳細は Unix システムの 「man 1 chmod」や「man 2 chmod」をご覧ください。

<?php
// 所有者に読み込み、書き込みの権限を与え、その他には何も許可しない。
chmod("/somedir/somefile", 0600);

// 所有者に読み込み、書き込みの権限を与え、その他には読み込みだけ許可する。
chmod("/somedir/somefile", 0644);

// 所有者に全ての権限を与え、その他には読み込みと実行を許可する。
chmod("/somedir/somefile", 0755);

// 所有者に全ての権限を与え、所有者が属するグループに読み込みと実行を許可する。
chmod("/somedir/somefile", 0750);
?>

戻り値

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

エラー / 例外

失敗時には E_WARNING が発生します。

注意

注意:

現在のユーザーは PHP を実行しているユーザーです。 これは普通のシェルや FTP アクセスでのユーザーとはたいてい違います。 たいていのシステムでは、ファイルの所有者のみがそのモードを 変更可能です。

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

参考

  • chown() - ファイルの所有者を変更する
  • chgrp() - ファイルのグループを変更する
  • fileperms() - ファイルのパーミッションを取得する
  • stat() - ファイルに関する情報を取得する

add a note

User Contributed Notes 6 notes

up
58
MethodicalFool
14 years ago
BEWARE, a couple of the examples in the comments suggest doing something like this:

chmod(file_or_dir_name, intval($mode, 8));

However, if $mode is an integer then intval( ) won't modify it. So, this code...

$mode = 644;
chmod('/tmp/test', intval($mode, 8));

...produces permissions that look like this:

1--w----r-T

Instead, use octdec( ), like this:

chmod(file_or_dir_name, octdec($mode));

See also: http://www.php.net/manual/en/function.octdec.php
up
38
Geoff W
14 years ago
BEWARE using quotes around the second parameter...

If you use quotes eg

chmod (file, "0644");

php will not complain but will do an implicit conversion to an int before running chmod. Unfortunately the implicit conversion doesn't take into account the octal string so you end up with an integer version 644, which is 1204 octal
up
36
masha at mail dot ru
19 years ago
Usefull reference:

Value Permission Level
400 Owner Read
200 Owner Write
100 Owner Execute
40 Group Read
20 Group Write
10 Group Execute
4 Global Read
2 Global Write
1 Global Execute

(taken from http://www.onlamp.com/pub/a/php/2003/02/06/php_foundations.html)
up
8
chris at ocproducts dot com
4 years ago
Windows has a very different file permission model to Unix and integrates them only minimally.

On Windows, all this function can do is to change the "read only" flag, which is turned on if $mode & 0200 does not pass.
i.e. it only checks if u+w is missing from the bitmask, and if it is, it sets the read only flag.

The executable flag cannot be set as Windows determines it based on file extension.
The write flag cannot be set as Windows determines write access based on ACLs, which are not integrated here.
up
6
alex at feidesign dot com
19 years ago
If you cannot chmod files/directories with PHP because of safe_mode restrictions, but you can use FTP to chmod them, simply use PHP's FTP-functions (eg. ftp_chmod or ftp_site) instead. Not as efficient, but works.
up
2
sander
15 years ago
if you want to chmod directories too, use this

<?php
$iterator
= new RecursiveIteratorIterator(new RecursiveDirectoryIterator($pathname), RecursiveIteratorIterator::SELF_FIRST);

foreach(
$iterator as $item) {
chmod($item, $filemode);
}
?>
To Top