In the operating system files, there is also a file attribute that we can define ourselves. These properties are not saved in the file content, nor are they directly visible through ls -al. They can permanently associate a key-value pair information to a file. Generally, today's Linux systems support such file extension properties. In the operating system, we can operate them through the commands setfattr, getfattr, and attr. Of course, PHP also provides us with an extension that can be used to operate on the extended properties of a file.
Add extended attributes
$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 the php file we are currently testing. Then use xattr_set() to set the file's extended properties. The extended attributes of files have the concept of namespace, and PHP also provides us with two forms: ordinary (user) namespace and XATTR_ROOT (root command space). The properties in the root namespace can be set by the super user and are not visible to other users. The user namespace is defined based on the permissions of the file, that is, the user who can currently operate the file can read the extended properties set by the user namespace of the file.
View the list of extended attributes
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 can get keys for all namespaces defined by a file. It also distinguishes between user and root namespaces.
Obtain 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"
The xattr_get() function is used to obtain the content of the extended attribute of the specified key. Coupled with the above xattr_list() function, you can get all the extended attribute information of a file. If we do not add the XATTR_ROOT parameter, we cannot read the content in the root namespace.
Delete extended attributes
var_dump(xattr_remove($file, 'Num.')); // bool(true) var_dump(xattr_list($file)); // array(1) { // [0]=> // string(6) "Author" // }
xattr_remove() is used to delete the extended attribute of the file. We directly deleted the Num. property in the user namespace of the test file. Check its xattr_list() again and there is only Author left. Similarly, this function also supports a third parameter to specify whether it is a root namespace operation.
Verify that the system supports extended attribute operation
var_dump(xattr_supported($file)); // bool(true)
Finally, there is a xattr_supported() function, which is used to verify whether the file system of the current operating system supports xattr-related operations.
Summarize
Today's content is very simple and simple. To be honest, I saw that there is this function extension in PHP and went back to view relevant documents in Linux system. Therefore, learning is all related. When we are learning PHP, we are also learning Linux, and we will often be exposed to related knowledge of applications such as MySQL and Nginx. Focusing on one area while expanding knowledge in other areas is the best way to learn.
Test code:
/zhangyue0503/dev-blog/blob/master/php/202010/source/9. Manipulate file extension properties in PHP.php
Reference documentation:
/manual/zh/
This is the article about operating extended attributes of files in PHP. For more related php extension attribute content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!