Manipulating extended properties of files in PHP


In the files of the operating system, there is also a file attribute that we can define ourselves. These attributes are not stored in the file content, nor can they be seen directly through LS – al. They can permanently associate a key value pair information to a file. Generally, current Linux systems support this function of file extension attributes. In the operating system, we can operate them through setfattr, getfattr and attr commands. Of course, PHP also provides us with an extension that can be used to operate on the extended attributes of files.

Add extended properties

$file = __FILE__;

var_dump(xattr_set($file, 'Author', 'ZyBlog')); // bool(true)
var_dump(xattr_set($file, 'Num.', 121 )); // bool(true)
var_dump(xattr_set($file, 'Description', 'shuo ming', XATTR_ROOT)); // bool(true)

First, we define the operation file, which we use directly here__ FILE__ Magic constants to manipulate the PHP file we are currently testing. Then use xattr_ Set() to set the extended attributes of the file. The extended attribute of the file has the concept of namespace, and PHP also provides us with a common (user) namespace and xattr accordingly_ Root (root command space) has two forms. The attributes in the root namespace can be set by the super user and not visible to other users, while the user namespace is defined according to the permissions of the file, that is, the user who can currently operate the file can read these extended attributes set by the user namespace of the file.

View extended attribute list

var_dump(xattr_list($file, XATTR_ROOT));
// array(1) {
//     [0]=>
//     string(11) "Description"
//   }

// array(2) {
//     [0]=>
//     string(4) "Num."
//     [1]=>
//     string(6) "Author"
//   }

xattr_ The list () function can get the keys of all namespaces defined by the file. It also distinguishes between the user and root namespaces.

Get extended attribute content

var_dump(xattr_get($file, 'Author')); // string(6) "ZyBlog"
var_dump(xattr_get($file, 'Description')); // bool(false)
var_dump(xattr_get($file, 'Description', XATTR_ROOT)); // string(9) "shuo ming"

xattr_ The get() function is used to obtain the extended property content of the specified key. With xattr above_ The list () function can get all the extended attribute information of a file. If we don’t add xattr_ With the root parameter, the contents in the root namespace cannot be read.

Delete extended properties

var_dump(xattr_remove($file, 'Num.')); // bool(true)
// array(1) {
//     [0]=>
//     string(6) "Author"
//   }

xattr_ Remove () is used to delete the extended attribute of the file. We directly deleted the num. attribute in the user namespace of the test file. Look at its xattr again_ List () leaves only author. Similarly, this function also supports the third parameter to specify whether it is the root namespace.

Verify whether the system supports extended attribute operation

var_dump(xattr_supported($file)); // bool(true)

Finally, there is an xattr_ The supported () function is used to verify whether the file system of the current operating system supports xattr related operations.


Today’s content is very simple and simple. To be honest, I went back to check the relevant documents in the Linux system after seeing the function extension in PHP. Therefore, learning is related. When we are learning PHP, we are also learning Linux. At the same time, we will often be exposed to the relevant knowledge of MySQL, nginx and other applications. The best way to learn is to focus on one field and expand knowledge in other fields.

Test code: Manipulating the extended properties of a file in PHP. PHP

Reference documents:


Official account: hard core project manager

Add wechat / QQ friends: [xiaoyuezigonggong / 149844827] get free PHP and project management learning materials

Tiktok, official account, voice, headline search, hard core project manager.

Station B ID: 482780532