(PECL xattr >= 0.9.0)

xattr_set Set an extended attribute


    string $filename,
    string $name,
    string $value,
    int $flags = 0
): bool

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

Extended attributes have two different namespaces: user and root. The user namespace is available to all users, while the root namespace is available only to users with root privileges. xattr operates on the user namespace by default, but this can be changed with the flags parameter.



The file in which we set the attribute.


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.


The value of the attribute.


Supported xattr 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.

Return Values

Returns true on success or false on failure.


Example #1 Sets extended attributes on .wav 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'));

See Also

add a note

User Contributed Notes 1 note

blr at blr dot hu
8 years ago
Be aware when use on filesystem without xattr support, it returns true. (Bug reported)
To Top