Let everyone learn the file system functions in PHP (1)

藏色散人
Release: 2023-04-10 11:02:01
forward
2920 people have browsed it

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

File path related functions are often more common in some frameworks, and are often used in conjunction with \_\_FILE\_\_ and \_\_DIR Use magic constants like \_\_.

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
Copy after login

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) .
Copy after login

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
Copy after login

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.

Modify file ownership-related information

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');
Copy after login

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
Copy after login

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
Copy after login

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. The 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 The previous 0 is used to ensure that the operation can be performed correctly. 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
// )
Copy after login

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.

Operations related to soft and hard file connections

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
// )
Copy after login

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 aimed 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
Copy after login

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.

Summary

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. In addition, operations related to directory paths are also the basis of some frameworks. Functions such as dirname() and basename() will be seen at the entrance of almost all frameworks or Composer.

测试代码:
https://github.com/zhangyue0503/dev-blog/blob/master/php/202010/source/6.PHP中的文件系统函数(一).php
参考文档:
https://www.php.net/manual/zh/ref.filesystem.php
Copy after login

推荐学习:《PHP视频教程

The above is the detailed content of Let everyone learn the file system functions in PHP (1). For more information, please follow other related articles on the PHP Chinese website!

Related labels:
php
source:segmentfault.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!