Starting from this article, we will learn a series of PHP file system related functions. In fact, many of these functions are often used by us. You don't need to remember them deliberately. As long as you know that there is such a thing, remember to check the documentation when using it.
File path related functions are often more common in some frameworks, and often cooperate with \_\_FILE\_\ Use magic constants such as _ and \_\_DIR\_\_.
echo "1) ".basename("/etc/sudoers.d", ".d"), PHP_EOL; echo "2) ".basename("/etc/passwd"), PHP_EOL; echo "3) ".basename("/etc/"), PHP_EOL; echo "4) ".basename("."), PHP_EOL; echo "5) ".basename("/"), PHP_EOL; echo "6) ".basename("/usr/local/Cellar/php/7.3.9_1/README.md"), PHP_EOL; // 1) sudoers // 2) passwd // 3) etc // 4) . // 5) // 6) README.md
basename() function is to get the file name in the path. It has two parameters. The first is the path of the file, and the second is the filtered content. For example, we filter the first test statement Remove the file extension.
echo "1) " . dirname("/etc/passwd") , PHP_EOL; echo "2) " . dirname("/etc/") , PHP_EOL; echo "3) " . dirname("."), PHP_EOL; // 1) /etc // 2) / // 3) .
dirname() returns the path part of the path, that is, the part that does not include the file name, which is exactly the opposite function of basename().
print_r(pathinfo('/usr/local/Cellar/php/7.3.9_1/README.md')); // Array // ( // [dirname] => /usr/local/Cellar/php/7.3.9_1 // [basename] => README.md // [extension] => md // [filename] => README // ) echo realpath('./../../..//../etc/passwd'), PHP_EOL; // /private/etc/passwd
pathinfo() function is used to return the information in the path in the form of an array. From the result, we can see the dirname part of the file, the basename part, and the file extension extension and no extension. The content of filename.
realpath() returns a normalized absolute path name. It expands all symbolic connections and processes ./, ../ and redundant / in the input path. The returned content is the absolute standard specification. path.
Next, we learn some functions that modify file-related attributes, mainly the operation of file permission information in the Linux system environment.
Of course, first we have to create a file. It is very similar to the command in Linux.
touch('test3.txt');
In addition to giving the name of the file to be created, the touch() function also has two optional parameters that can specify the creation time and access time of the file. If no parameters are given, the default is the current time. This filename can be a relative or absolute path to a directory with permissions, and creates an empty file in that directory.
echo fileowner('test.txt'), PHP_EOL; // 501 chown('test.txt', 'www'); clearstatcache(); echo fileowner('test.txt'), PHP_EOL; // 70
Through the fileowner() function, we can get the user to which a file belongs. By default, our user is the user currently running the PHP script, which is the current logged-in user of the system.
Here, we use the chown() function to change the user to www user. clearstatcache() is used to clear the cache information of the file system. If it is not cleaned, fileowner() will still return the previous user information.
echo filegroup('test.txt'), PHP_EOL; // 20 chgrp('test.txt', 'www'); clearstatcache(); echo filegroup('test.txt'), PHP_EOL; // 70 echo substr(sprintf('%o', fileperms('test.txt')), -4), PHP_EOL; // 0766 chmod('test.txt', 0777); clearstatcache(); echo substr(sprintf('%o', fileperms('test.txt')), -4), PHP_EOL; // 0777
Similarly, use the filegroup() function to obtain the file's group information, and chgrp() is used to modify the file's group. fileperms() is used to return the permission information of the file. It returns the file access permission in numerical mode. Here we use sprintf() to format the result and obtain our commonly used Linux system permission format.
chmod() function is used to modify the permissions of files. Its permission parameters are three numbers composed of octal data, which represent 1, 2, 4 and their combinations in the Linux system, so We need to add a 0 in front to ensure that the operation can be performed normally. Regarding the knowledge of system file permissions, everyone needs to carefully study the relevant content in the Linux system.
Note that if the above function fails to run on the command line, most of the reasons are due to lack of permissions. You can use sudo to test. When running in fastcgi, you need to pay more attention to permission issues and make safe file permission modifications only in directories that our server can operate.
print_r(stat('test.txt')); // Array // ( // [0] => 16777220 // [1] => 8707958352 // [2] => 33279 // [3] => 2 // [4] => 70 // [5] => 70 // [6] => 0 // [7] => 0 // [8] => 1603070453 // [9] => 1603070453 // [10] => 1603072836 // [11] => 4096 // [12] => 0 // [dev] => 16777220 // [ino] => 8707958352 // [mode] => 33279 // [nlink] => 2 // [uid] => 70 // [gid] => 70 // [rdev] => 0 // [size] => 0 // [atime] => 1603070453 // [mtime] => 1603070453 // [ctime] => 1603072836 // [blksize] => 4096 // [blocks] => 0 // )
The stat() function can obtain all attribute information of the specified file. Here we can see the uid, gid, ctime, mtime and other information of the file.
In the Linux system, there is relevant knowledge about soft connections and hard connections. In fact, soft links are like shortcuts in Windows, while hard links are related to copying a copy of data. In PHP, we also provide us with the creation of soft and hard connections and some related operations.
link('test.txt', 'ltest.txt'); echo linkinfo('ltest.txt'), PHP_EOL; // 16777220 symlink('test.txt', 'ltest2.txt'); echo linkinfo('ltest2.txt'), PHP_EOL; // 16777220 print_r(lstat('ltest2.txt')); // Array // ( // [0] => 16777220 // [1] => 8707962848 // [2] => 41453 // [3] => 1 // [4] => 0 // [5] => 20 // [6] => 0 // [7] => 8 // [8] => 1603072717 // [9] => 1603072717 // [10] => 1603072717 // [11] => 4096 // [12] => 0 // [dev] => 16777220 // [ino] => 8707962848 // [mode] => 41453 // [nlink] => 1 // [uid] => 0 // [gid] => 20 // [rdev] => 0 // [size] => 8 // [atime] => 1603072717 // [mtime] => 1603072717 // [ctime] => 1603072717 // [blksize] => 4096 // [blocks] => 0 // )
Using the link() function creates a hard link file of the specified file, while using symlink() creates a soft link file. Relatively speaking, we use soft connections in more scenarios. lstat() has the same function as the stat() function, viewing various attribute information of the file, but the lstat() function is targeted at soft and hard link files.
lchown('ltest2.txt', 'zhangyue'); lchgrp('ltest2.txt', 'staff'); // lrwxr-xr-x 1 zhangyue staff 8 Oct 19 09:58 ltest2.txt -> test.txt
Similarly, we can also modify the user and user group information of soft and hard connections, but their information cannot be viewed through fileowner() or filegroup(). Because they are connection files, they are still bound to the original file. Using functions such as fileowner(), you can still view the information of the original file. We can use ls -l in the system environment to check whether the user and user group information of the connection file has been modified successfully.
Today’s content is relatively simple, and the operation of modifying permissions is not commonly used. However, they are still very useful for system security. For example, for uploading, if we want to prevent the uploading of executable files, we can modify the permissions of the files to prevent the files from being run directly, thus playing a role in security protection.
另外,目录路径相关的操作也是一些框架的基础,几乎所有框架的入口或者说是 Composer 的入口,都会见到 dirname() 以及 basename() 之类函数的身影。
测试代码:
https://github.com/zhangyue0503/dev-blog/blob/master/php/202010/source/6.PHP中的文件系统函数(一).php
推荐学习:php视频教程
The above is the detailed content of What are the file system functions in PHP?. For more information, please follow other related articles on the PHP Chinese website!