Starting with this article, we’ll look at a number of PHP file system-related functions. In fact, many of these functions are often used by us. We do not need to remember them deliberately. As long as we know that there is such a thing, we can remember to check the documentation when we use it.

File path correlation function

File-path-related functions are common in some frameworks and are often used with magic constants such as \_\_FILE\_\_, \_\_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)". The basename ("/usr/local/Cellar/PHP / 7.3.9 _1 / README. Md "), PHP_EOL; // 1) sudoers // 2) passwd // 3) etc // 4) . // 5) // 6) README.md

The basename() function gets the name of the file in the path. It takes two arguments, the first is the path to the file and the second is the content to filter out. For example, in the first test statement we filter out the suffix name of the file.

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 of the path that does not contain the filename, which is the opposite 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 / / / the basename = > README. Md / / [extension] = > / / md [filename] => README // ) echo realpath('./.. /.. /.. / /.. /etc/passwd'), PHP_EOL; // /private/etc/passwd

The pathInfo () function is used to return the information in the path as an array. From the results, we can see the dirname part of the file, the basename part of the file, the extension of the file, and the filename content without the extension.

RealPath () returns the normalized absolute pathname, which extends all symbolic connections and handles the./,.. in the input path. / and the superfluous /, returns the absolute path to the standard specification.

Modify file ownership information

Next, we’ll look at some functions that modify file-related properties, mainly file permission information in the context of a Linux system.

Of course, first we have to create a file. It’s very similar to a command in Linux.

touch('test3.txt');

In addition to giving the file name to be created, the touch() function has two optional arguments that specify when the file was created and when it was accessed. Otherwise, the default is the current time. The file name can be a directory with permissions in the relative or absolute path, and an empty file is created under that directory.

echo fileowner('test.txt'), PHP_EOL; // 501 chown('test.txt', 'www'); clearstatcache(); echo fileowner('test.txt'), PHP_EOL; / / 70

With 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, that is, the user currently logged in to the system. Here, we use the chown() function to change the user to a WWW user. ClearStatCache () is used to clean up the file system cache. If not cleaned up, fileOwner () returns the same user information as before.

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, the fileGroup () function is used to get the generic group information of a file, and CHGRP () is used to modify the generic group of a file. Fileperms () is used to return the file permission information. It returns the file access rights in digital mode. Here we use sprintf() to format the results and get the usual Linux system permission format. The chmod() function is used to modify the permissions of a file. It takes three hexadecimal numbers that represent 1, 2, 4, and their combination on Linux, so we need to add a 0 in front to make sure the operation works. You need to learn a lot about system file permissions in Linux.

Note that if the above function fails on the command line, most of the reason is because you don’t have permissions, you can test it using sudo. When running in FastCGI, it’s even more important to be aware of permissions and make secure file permissions 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 retrieve all the attributes of the specified file. Here we can see the file’s uid, gid, ctime, mtime, and so on.

Hard and soft file connection related operations

In Linux systems, there is knowledge of both soft and hard connections. A soft connection is like a shortcut in Windows, and a hard connection is about copying a piece of data. In PHP, we also provide the creation of hard and soft connections and 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 / /)

The link() function creates a hard-wired file with the specified file, while the symlink() function creates a soft-wired file. Relatively speaking, we use soft connections in more scenarios. Lstat () looks at various attributes of a file just like the stat() function does, but lstat() works on hard and soft 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 modify user and user group information for hard and soft connections, but their information cannot be viewed through fileOwner () or fileGroup (). Because they are connected files and are themselves bound to the original file, using a function like fileOwner () you can still see information about the original file. We can use ls -l in the system environment to see if the user and user group information of the connection file has been modified successfully.

conclusion

Today’s content is relatively simple, and modifying permissions is not very common. However, for system security, they are still very useful, such as for uploading, we want to prevent uploading executable files, we can modify the file permissions to make the file can not run directly, so as to play a role in security protection. In addition, directory path-related operations are also the basis of some frameworks, and almost every framework entry, or Composer entry, will see functions like dirname() and basename().

Test code:

File system function of https://github.com/zhangyue0503/dev-blog/blob/master/php/202010/source/6.PHP (a). The PHP

Reference documents:

https://www.php.net/manual/zh/ref.filesystem.php

Search for “Hardcore Project Manager” on all media platforms