This time we’re going to look at some of the less common, but also very useful functions. Some of them you may have seen or used, some of them you may not really remember. They are all part of the file system-related operation functions in PHP. Existing is reasonable, maybe we just haven’t been exposed to it in our business development. Whatever else, get familiar with them, and when you really need them you’ll remember that PHP comes with a function like this.

Directory judgment, create, delete, path cache information

var_dump(is_dir("./")); // bool(true)
var_dump(disk_free_space("./")); // float(7727517696)
var_dump(disk_total_space("./")); // float(250790436864)

The is_dir() function is used to determine whether a given path exists or is normal. It is common to use this function to verify that the directory has been created before manually creating the directory. This is common in business scenario development related to uploading. The disk_free_space() and disk_total_space() functions get the remaining disk space and the total disk space data for the specified directory. Since my computer is running on a Mac, the total disk space is 250 gigabytes, and the available space is 7 gigabytes, so I’m ready to clean my computer soon.

var_dump(mkdir("./a")); // bool(true)
var_dump(rmdir("./a"));  // bool(true)

The mkdir() function is used to create a directory. In addition to the given path parameter, it has an optional parameter to set the file permissions of the directory, and also passes a value such as 0777. This function is not familiar to you. Most of the ability to upload files into directories is done using is_dir() in conjunction with mkdir() for directory creation. Rmdir () is used to delete a directory. This function has two preconditions: the directory to be deleted must be empty, and it must have permission to delete the directory. If either condition is not met, an E_WARNING level error will be reported.

realpath('./'); var_dump(realpath_cache_get()); {/ / / / array (8) ["/Users/zhangyue/MyDoc/blog post/dev blog/PHP / 202010 / source "] = > / / array (4) {/ / / "key" = > / / Float (1.4990943845035 e+19) / / / "is_dir" = > / / bool (true) / / / "realpath" = > / / string (61). // ["expires"]=> // int(1603327834) //} // ["/Users/zhangyue/MyDoc/blog posts "] = > / / array (4) {/ / / "key" = > / / int (8597410586338680) / / / "is_dir" = > / / bool (true) / / [" realpath "] = > / / string (34) "/ Users/zhangyue/MyDoc/blog" / / / "expires" = > / / int (1603327834) / /} / / / / Users "= > / /...  / /... var_dump(realpath_cache_size()); // int(673)

The realpath_cache_get() function is used to get the details of the real directory cache. We need to first get a directory path using realpath(), and then we can see the contents of realpath_cache_get(). It can be seen that the array it returns is the path from the first directory to this directory all directory information, including each level of directory property information such as realpath, is_dir. Realpath_cache_size () gets the size of the realpath buffer, which is the memory usage of the realpath cache size.

Soft connection information

In the previous article, we learned how to create a connection file, so let’s take a look at two small functions for connection information.

var_dump(readlink('ltest2.txt')); // "test.txt"
var_dump(is_link('ltest2.txt')); // bool(true)

The readlink() function is used to get the name of the real file to which the soft connection is connected. In the previous article, the ltest2.txt file we created is a soft connection to the test.txt file. The is_link() function is used to determine whether the given file is a connection file.

Copy, move, rename, delete file operations

var_dump(copy('test.txt', 'cp_test.txt')); // bool(true)

var_dump(is_file("cp_test.txt")); // bool(true)

var_dump(move_uploaded_file('test.txt', 'mv_upload_test.txt')); // bool(false)
var_dump(is_file("mv_upload_test.txt")); // bool(false)
var_dump(is_uploaded_file("mv_upload_test.txt")); // bool(false)

var_dump(copy('test.txt', 're_test.txt')); // bool(true)
var_dump(rename('re_test.txt', 'new_re_test.txt')); // bool(true)

var_dump(copy('test.txt', 'del_test.txt')); // bool(true)
var_dump(unlink("del_test.txt")); // bool(true)

Copy () is a very common function for files. Copy-and-paste operations are central to our work, whether we’re writing code or in the office. The copy() function in PHP is specifically used for copying files, but be sure to have read and write permissions to both the file and the directory to which the copy is to be made.

The move_uploaded_file() file is also familiar, and is often used in the operation of uploading files. Note that move_uploaded_file() is used to move the uploaded files from the TMP in the $_FILES file. It cannot be used as copy(). As you can see from the demo code, it is impossible to copy and move regular files. The is_uploaded_file() function is used to determine whether the file to be manipulated is a PHP uploaded file.

Through the above two functions, I believe many people will think of their use in the file upload, here is a simple pseudocode.

if(! Move_uploaded_file (' XXXX ', 'XXXX')) {if (copy (' XXXX ', 'XXXX')) {/ / upload success} else {/ / upload failed}}

You’ll find this in many tutorials, and even in some frameworks. To do this, use move_uploaded_file() to move the uploaded file, and if that fails, use copy(). If it still fails, the entire upload operation is considered to have failed.

The rename() function is used to rename a file, which is similar to the mv command on Linux.

The is_file() function is used to determine whether a given file is a normal file. In the operating system, especially in the Linux system, all documents, so this function is the most commonly used real scene is judging whether a given path is a directory or a file, a lot of times we will use it to judge after the success of the upload file is normal, or judgment of a given path is a directory or a file.

Finally, there is the unlink() function. In PHP, there is no function such as delete or rm, and unlink() is used to delete files. However, its name sounds like a way to unconnect symbolic link files. Both connection files and normal files are deleted using the unlink() function.

File read at one time

We’ve already covered how to read a file at a time and how to stream it in bytes or lines. You can check this out in PHP Large File Reading. So here we simply post the code to demonstrate it.

var_dump(file_exists('test.txt')); // bool(true)
var_dump(readfile('test.txt')); // asdfasdfint(8)
// array(1) {
//     [0]=>
//     string(8) "asdfasdf"
//   }

$c = file_get_contents('test.txt');
var_dump($c); // string(8) "asdfasdf"

var_dump(file_put_contents('fpc_test.txt', $c)); // int(8)

The file_exists() function is a professional check for the existence of a file, and is_file() above is more used after uploading. File_exists (), on the other hand, is a very common function in everyday coding. ReadFile () reads the contents of a file directly into the contents. File_get_contents () does the same thing, except that file_get_contents() returns the contents of a file as a string. If you look at our var_dump() result, file_get_contents() is clearly marked as a string(8), whereas readFile () outputs the contents directly without buffers, meaning that it is a function like phpInfo (). You need to use a function like ob_start() to put the contents of the readFile () function into a variable. Our previous article focused on the concept of buffers, the output buffering control in PHP. Its return value is the number of bytes of the file, followed by int(8).

The file() function stores the contents of the file in an array, which is delimited by default into rows, meaning that each line is divided into one element of the array.

File_put_contents () writes the given contents to a file, and in combination with file_get_contents() can also make a copy.

File attributes

var_dump(fileatime('test.txt')); // int(1603243708)
var_dump(filectime('test.txt')); // int(1603242166)
var_dump(filemtime('test.txt')); // int(1603242166)

var_dump(fileinode('test.txt')); // int(8707958352)
var_dump(filesize('test.txt')); // int(8)
var_dump(filetype('test.txt')); // string(4) "file"

var_dump(is_executable('test.txt')); // bool(true)
var_dump(is_writable('test.txt')); // bool(true)
var_dump(is_readable('test.txt')); // bool(true)

Obviously, fileAtime (), filecTime (), and filemTime () correspond to the last time a file was accessed, the inode modified time, and the modified time, which are the same three concepts associated with files in Linux systems. FileInode () is used to get the inode information for the file. FileSize () is the size of the file, and fileType () is the type information of the file.

Is_executable () determines whether the file can be executed, and is_writable() and is_readable() determine whether the file can be written and read. These three functions correspond to the file permissions related judgment.

Create unnamed files and temporary files

var_dump(tempnam('./', 't_')); // String (70) "/Users/zhangyue/MyDoc/ blog/ dev-blog/ PHP /202010/source/t_Gx6S5d" $temp = tmpfile(); fwrite($temp, "writing to tempfile"); fseek($temp, 0); // sleep(30); // TMP /phpU2LZ3V echo fread($temp, 1024), PHP_EOL; // writing to tempfile fclose($temp); // The file is deleted

The tempnam() function generates a random, unnamed, empty file with the specified prefix argument. In the test code, we give the prefix a value of t_, and the resulting file is an empty file called t_Gx655d.

As explained in the previous article, tmpfile() generates a temporary file, which is normally placed in/TMP (if you haven’t changed the php.ini Settings). This function creates the file and returns a handle that is immediately deleted once the file is closed with fclose().

Returns the contents of the directory as per the rule

Foreach (glob("*.txt") as $filename) {echo "$filename size: ". Filesize ($filename), PHP_EOL; } // cp_test.txt size 8 // fpc_test.txt size 8 // ltest.txt size 8 // ltest2.txt size 8 // new_re_test.txt size 8 // test.txt size 8 // test3.txt size 0 foreach (glob(".. /.. /*.md") as $filename) {echo "$filename size: ". Filesize ($filename), PHP_EOL; } / /. /.. / 202009/1. In PHP PDO operation learning classes and binding data (3) pretreatment. Md size: 16881 / /.. /.. // 202009/10.php: Calendar extension //... /.. // 202009/11. Learn the internationalization feature in PHP to view currency and date information. /.. // 202009/12.PHP: date-related function. MD size: 14217 //.. /.. // 202009/13.PHP // Date-related functions. MD size: 9858 //.. /.. / 202009/2. PHP PDO operating learning (4) in a query structure set. Md size: 12825 / /.. /.. // 202009/3. Convert XML to Array in PHP using Object Method in SPL. MD Size: 6068 //.. /.. / 202009/4. In PHP MySQLi extension study (a) MySQLi is introduced. The md size: 6029 / /.. /.. / 202009/5. In PHP MySQLi extension learning (2) the MySQLi class methods of some rare attribute. Md size: 9726 / /.. /.. / 202009/6. In PHP MySQLi extension learning MySQLi (3) of the basic operation. Md size: 9403 / /.. /.. / 202009/7.php // MySQLi // MySQLi // MySQLi // MySQLi // /.. // 202009/8.php // MySQLI_STMT // MySQLI_STMT // MySQLI_STMT // // // // // // // // // // // // // // // // // // // /... /.. / 202009/9.php/MySQLI_result/MySQLI_result/MySQLI_result/MySQLI_result/MySQLI_result/MySQLI_result

The glob function is also a previously explained function that returns information about all files or directories in a directory according to a specified rule. Can be conveniently used for directory traversal operations. Note that the rule parameters here are not full regular expressions, and you can consult the documentation for the exact syntax it supports.

File umask operation

$old = umask(0); echo $old, PHP_EOL; // 18 $now = umask(); echo $now, PHP_EOL; / / 0

The umask() function operates on the umask information of the currently executing process. Like the umask command in Linux, it is used to specify the default permissions of the currently created directory file. In PHP, umask() sets the PHP umask to Mask & 0777 and returns the original umask. When PHP is used as a server module, the umask is restored after each request. For specific umask knowledge, you can refer to the relevant content in Linux.

Configuration file information read

The last two functions are used to read PHP-type configuration file information. What is PHP-type configuration file information? This is a configuration file similar to the php.ini file, with key=value. For example, Laravel’s.env file can be read using these two functions.

Var_dump (the parse_ini_file ('/usr/local/etc/PHP / 7.3 / PHP ini ')); // array(133) { // ["#zend_extension"]=> // string(9) "" // ["extension"]=> // string(6) "" // [" engine "] = > / /... / /... Var_dump (the parse_ini_file ('/usr/local/etc/PHP / 7.3 / PHP ini ', true)); // array(38) { // ["#zend_extension"]=> // string(9) "" // ["extension"]=> // string(6) "" // ["PHP"]=> / / array (45) {/ / / "engine" = > / / string (1) "1" / / / "short_open_tag" = > / /... / /...

The parse_ini_file() function reads the contents of the configuration file directly from the specified path. In this case, we directly test reading the php.ini file. It has an optional argument that, if set to true, returns the structured contents of the array classification.

$ini = file_get_contents ('/usr/local/etc/PHP / 7.3 / PHP ini '); var_dump(parse_ini_string($ini)); // array(133) { // ["#zend_extension"]=> // string(9) "" // ["extension"]=> // string(6) "" // [" engine "] = > / /... / /... var_dump(parse_ini_string($ini, true)); // array(38) { // ["#zend_extension"]=> // string(9) "" // ["extension"]=> // string(6) "" // ["PHP"]=> / / array (45) {/ / / "engine" = > / / string (1) "1" / / / "short_open_tag" = > / /... / /...

Parse_ini_string () reads configuration information from a given string, and also has an optional parameter to format the grouped output. This is exactly the same as parse_ini_file(), the only difference being that one reads from the file path and one reads from the string.


I’ve introduced a lot of functions in one go. Have you all used them? Some people say, why do you write this? Why don’t you just go to the documentation? That is not the same as oh, a lot of functions in the document on the introduction of a sentence, the example code is also the use of English annotation, although we this is also a porter, but we are not only a simple handling, but also the example rewrite, in addition to the introduction of some application scenarios! As for you to see the official feeling good, that is benevolence, wisdom!

Test code: function in the file system (2). The PHP

Reference documents:

= = = = = = = = = = =

Search for “Hardcore Project Manager” on all media platforms