In the operating system files, there is also a file attribute that we can define ourselves. These properties are not stored in the contents of the file, nor are they directly visible through LS-AL. They can permanently associate a key-value pair information to a file, which is generally supported on current Linux systems. In the operating system we can operate them with setfattr, getfattr, attr and so on. Of course, PHP also provides an extension that you can use to manipulate the extended properties of a file.

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, where we directly use the \_\_FILE\_\_ magic constant to operate on the PHP file we are currently testing. You can then use xattr_set() to set the extended properties of the file. The extended properties of files have the concept of namespaces, and PHP provides us with both the common (user) namespace and the XATTR_ROOT (root command space). Properties in the root namespace can be set by the superuser and not visible to other users, while the user namespace is defined according to the permissions of the file. This means that the extended properties set by the user namespace of the file can be read by the user that currently operates on the file.

View the list of extended properties

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

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

The xattr_list() function gets the keys of all the namespaces defined by the file. It also differentiates between user and root namespaces.

Gets the extended property 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"

The xattr_get() function is used to get the extended property content for the specified key. With the above xattr_list() function, you can get all the extended property information for a file. If we do not add the XATTR_ROOT parameter, we cannot read the contents of the root namespace.

Delete extended properties

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

Xattr_remove () is used to remove the extension property of the file. We directly remove Num from the user namespace of the test file. Properties. A second look at its xattr_list() leaves only the Author. Similarly, this function supports a third argument to specify whether the operation is root namespace.

Verify that the system supports extended property operations

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

Finally, there is an xattr_supported() function that verifies that the current operating system’s file system supports Xattr-related operations.

conclusion

Today’s content is very simple and simple. To be honest, the function of the extension property of this file is not until I saw this function extension in PHP, I went back to check the related documentation of Linux system. Therefore, learning is all related. When we learn PHP, we also learn Linux. At the same time, we often come into contact with relevant knowledge of MySQL, Nginx and other applications. Focusing on one area while expanding your knowledge in others is the best way to learn.

Test code:

https://github.com/zhangyue0503/dev-blog/blob/master/php/202010/source/9. Manipulate file extension properties in PHP. PHP

Reference documents:

https://www.php.net/manual/zh/book.xattr.php

= = = = = = = = = = =

Search for “Hardcore Project Manager” on all media platforms