xattr_get

Get an extended attribute

说明

string xattr_get ( string $filename , string $name [, int $flags = 0 ] )

This function gets the value of an extended attribute of a file.

扩展的属性有两种 不同的命名空间:user 和 root。user 命名空间对所有用户均有效,而 root 命名空间仅对拥有 root 权限的用户有效。 xattr 默认在 user 命名空间上操作,但可使用 flags 参数进行更改。

参数

filename
The file from which we get the attribute.

name
The name of the attribute.

flags
| | | |------------------------|----------------------------------------------------------------------| | XATTR_DONTFOLLOW | Do not follow the symbolic link but operate on symbolic link itself. | | XATTR_ROOT | Set attribute in root (trusted) namespace. Requires root privileges. |

返回值

Returns a string containing the value or FALSE if the attribute doesn't exist.

范例

示例 #1 Checks if system administrator has signed the file

<?php
$file = '/usr/local/sbin/some_binary';
$signature = xattr_get($file, 'Root signature', XATTR_ROOT);

/* ... check if $signature is valid ... */

?>

参见

  • xattr_list
  • xattr_set
  • xattr_remove

xattr_list

Get a list of extended attributes

说明

array xattr_list ( string $filename [, int $flags = 0 ] )

This functions gets a list of names of extended attributes of a file.

扩展的属性有两种 不同的命名空间:user 和 root。user 命名空间对所有用户均有效,而 root 命名空间仅对拥有 root 权限的用户有效。 xattr 默认在 user 命名空间上操作,但可使用 flags 参数进行更改。

参数

filename
The path of the file.

flags
| | | |------------------------|----------------------------------------------------------------------| | XATTR_DONTFOLLOW | Do not follow the symbolic link but operate on symbolic link itself. | | XATTR_ROOT | Set attribute in root (trusted) namespace. Requires root privileges. |

返回值

This function returns an array with names of extended attributes.

范例

示例 #1 Prints names of all extended attributes of file

<?php
$file = 'some_file';
$root_attributes = xattr_list($file, XATTR_ROOT);
$user_attributes = xattr_list($file);

echo "Root attributes: \n";
foreach ($root_attributes as $attr_name) {
    printf("%s\n", $attr_name);
}

echo "\n User attributes: \n";
foreach ($attributes as $attr_name) {
    printf("%s\n", $attr_name);
}

?>

参见

  • xattr_get

xattr_remove

Remove an extended attribute

说明

bool xattr_remove ( string $filename , string $name [, int $flags = 0 ] )

This function removes an extended attribute of a file.

扩展的属性有两种 不同的命名空间:user 和 root。user 命名空间对所有用户均有效,而 root 命名空间仅对拥有 root 权限的用户有效。 xattr 默认在 user 命名空间上操作,但可使用 flags 参数进行更改。

参数

filename
The file from which we remove the attribute.

name
The name of the attribute to remove.

flags
| | | |------------------------|----------------------------------------------------------------------| | XATTR_DONTFOLLOW | Do not follow the symbolic link but operate on symbolic link itself. | | XATTR_ROOT | Set attribute in root (trusted) namespace. Requires root privileges. |

返回值

成功时返回 TRUE, 或者在失败时返回 FALSE

范例

示例 #1 Removes all extended attributes of a file

<?php
$file = 'some_file';
$attributes = xattr_list($file);

foreach ($attributes as $attr_name) {
    xattr_remove($file, $attr_name);
}
?>

参见

  • xattr_list
  • xattr_set
  • xattr_get

xattr_set

Set an extended attribute

说明

bool xattr_set ( string $filename , string $name , string $value [, int $flags = 0 ] )

This function sets the value of an extended attribute of a file.

扩展的属性有两种 不同的命名空间:user 和 root。user 命名空间对所有用户均有效,而 root 命名空间仅对拥有 root 权限的用户有效。 xattr 默认在 user 命名空间上操作,但可使用 flags 参数进行更改。

参数

filename
The file in which we set the attribute.

name
The name of the extended attribute. This attribute will be created if it doesn't exist or replaced otherwise. You can change this behaviour by using the flags parameter.

value
The value of the attribute.

flags
| | | |------------------------|----------------------------------------------------------------------| | XATTR_CREATE | Function will fail if extended attribute already exists. | | XATTR_REPLACE | Function will fail if extended attribute doesn't exist. | | XATTR_DONTFOLLOW | Do not follow the symbolic link but operate on symbolic link itself. | | XATTR_ROOT | Set attribute in root (trusted) namespace. Requires root privileges. |

返回值

成功时返回 TRUE, 或者在失败时返回 FALSE

范例

示例 #1 Sets extended attributes on .wav file

<?php
$file = 'my_favourite_song.wav';
xattr_set($file, 'Artist', 'Someone');
xattr_set($file, 'My ranking', 'Good');
xattr_set($file, 'Listen count', '34');

/* ... other code ... */

printf("You've played this song %d times", xattr_get($file, 'Listen count')); 
?>

参见

  • xattr_get
  • xattr_remove

xattr_supported

Check if filesystem supports extended attributes

说明

bool xattr_supported ( string $filename [, int $flags = 0 ] )

This functions checks if the filesystem holding the given file supports extended attributes. Read access to the file is required.

参数

filename
The path of the tested file.

flags
| | | |------------------------|----------------------------------------------------------------------| | XATTR_DONTFOLLOW | Do not follow the symbolic link but operate on symbolic link itself. |

返回值

This function returns TRUE if filesystem supports extended attributes, FALSE if it doesn't and NULL if it can't be determined (for example wrong path or lack of permissions to file).

范例

示例 #1 xattr_supported example

The following code checks if we can use extended attributes.

<?php
$file = 'some_file';

if (xattr_supported($file)) {
    /* ... make use of some xattr_* functions ... */
}

?>

参见

  • xattr_get
  • xattr_list

目录