CakeFest 2024: The Official CakePHP Conference

posix_access

(PHP 5 >= 5.1.0, PHP 7, PHP 8)

posix_access ファイルのアクセス権限を判断する

説明

posix_access(string $filename, int $flags = 0): bool

posix_access() は、ファイルに対するユーザーの アクセス権限を調べます。

パラメータ

filename

調べるファイルの名前。

flags

POSIX_F_OKPOSIX_R_OKPOSIX_W_OK および POSIX_X_OK のうちのひとつあるいは複数からなるマスク。

POSIX_R_OKPOSIX_W_OK および POSIX_X_OK は、ファイルが存在して読み込み/書き込み/ 実行の権限があるかどうかを調べます。 POSIX_F_OK は単にファイルが存在するかどうか だけを調べます。

戻り値

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

例1 posix_access() の例

この例は $file が読み書き可能であるかどうかを調べ、 そうでない場合にエラーメッセージを表示します。

<?php

$file
= 'some_file';

if (
posix_access($file, POSIX_R_OK | POSIX_W_OK)) {
echo
'このファイルの読み込みと書き込みが可能です!';

} else {
$error = posix_get_last_error();

echo
"エラー $error: " . posix_strerror($error);
}

?>

注意

参考

  • posix_get_last_error() - 直近で失敗した posix 関数が設定したエラー番号を取得する
  • posix_strerror() - 指定したエラー番号に対応するシステムのエラーメッセージを取得する

add a note

User Contributed Notes 1 note

up
-1
unixguy at earth dot com
9 years ago
It should be noted that this function performs access checks based on the real UID and real GID of the process running PHP. These aren't necessarily the same as the effective UID and GID.

In other words, it may well be that access() returns “true” for a particular permission, but an fopen() operation which requires the same permission will fail, and vice versa.

Keep that in mind if you use access() for such checks.
To Top