Home > Article > Backend Development > A detailed discussion of the basic operations of PHP file directories_PHP Tutorial
We know that temporarily declared variables are stored in memory, even static variables will be released after the script is finished running. So, if you want to save the contents of a variable for a long time, one way is to write it to a file and put it on the hard disk or server. For this, you must be familiar with file operations.
1. Obtain the attribute information of the file
First of all, files have types. Under Linux, there are block (block devices, such as disk partitions, CD-ROMs), char (devices that use characters as input, such as keyboards and printers), and dir (directory types, which are also files. A kind of), fifo (named pipe, the explanation is to transfer information from one process to another process), file (ordinary file), link (link, similar to the shortcut under win), unknown (unknown type) 7 major Class, under win, there are only 3 classes: file, dir and unknown. Linux scumbag said that he must work hard on Linux-_-, he was born for Linux.
There are several functions for obtaining types: filetype: obtain the type; is_file: determine whether it is a normal file; is_link: determine whether it is a link.
There are several functions for obtaining attributes:
file_exists: Determine whether the file or directory exists;
filesize: Get the file size;
is_readable, is_writable, is_executable: whether it is readable, writable, and executable;
filectime, filemtime, fileatime: Get the creation time (create), modification time (modify), and access time (access) of the file, all returning timestamps;
stat: Get some basic information of the file and return a mixed array of index and association.
For example, you can determine the file type like this:
The code is as follows:
filesize returns data in bytes. If the file number is large or very large, you can process the number first. The code is as follows
The code is as follows:
Now to get the file information comprehensively, the code is as follows:
The code is as follows:
The effect is as follows:
File permissions, group and other functions are also used in the code. It is necessary to explain it (please correct it if it is wrong). The permissions of a file are divided into readable, writable and executable. It is generally expressed as: rwx. The corresponding letters indicate readable, writable and executable. The specified values from front to back are 4, 2, 1. The result of adding the three values is the largest. is 7, so 0666 is expressed in octal, which looks very convenient. If it is 7, it means that this file has these three permissions, so why is 0666 printed? We all know that there is a user under Windows. Under Linux, similar to Windows, there is also a user logged in, so a file may be owned by the user. A user also has its own group and the system. There are other groups in the file (guessing that this division should be a management need), so for 0666, the first 6 represents the user's permissions on the file, and the second 6 represents the user's group's permissions on the file. Permissions, the third 6 indicates the permissions of other groups (so that you don’t have to distinguish other users except this group one by one), 6 means that the file is readable and writable (you will know if it is executable under win) is an .exe file).
2. Directory operations
Directory reading, opendir: open a directory and return a handle pointing to the content in the directory. If the content in the directory is regarded as sequential data, such as an array arranged in order, this handle will Points to the beginning of this array. In fact, the system will sort the contents of this directory according to dictionary, whether it is a file or a subdirectory. readdir: Read the contents of the next directory, return the file name, and automatically point to the next file/directory in the directory, so reading the contents of a directory, excluding the contents of subdirectories, requires a loop to control, in After reading, the handle variable must be closed. The same is true when C language reads files. Open and close. Take my machine as an example:
The code is as follows:
The effect is as follows:
You can see that the system actually sorts the contents of the directory in a dictionary that ignores case.
To calculate the size of the directory, we know that the size of the file can be obtained by filesize, but there is no function in PHP that specifically calculates the size of the directory. Of course, there are functions disk_total_space (calculating the total hard disk space) and disk_free_space (calculating the available hard disk space) in PHP to calculate the size of the hard disk, but I tried disk_free_space and it seemed that the calculation was wrong. Because filesize calculates the size of a file, recursion needs to be used. When it is a directory, go in and continue to calculate the size of the subdirectory. If it is a file, get the file size and add the return. The code is as follows:
The code is as follows:
I created a size file on the F drive and randomly created some subdirectories and documents. The effect is as follows. The left side is obtained by the program, and the right side is obtained by right-clicking to view the folder properties for comparison.
The creation and deletion of directories are mainly used, mkdir: create a new directory, rmdir: delete a non-empty directory, note that it can only be non-empty, the code is as follows:
The code is as follows:
Then the question is, what if you want to delete a non-empty directory? You have to write it yourself. The idea is still recursive, because PHP only provides the delete file function unlink, so when deleting a directory, opendir first, and then Enter, if it is a file, delete it directly. If it is a directory, continue to enter and use this method to process. Of course, a bool variable can be returned to indicate whether the deletion is successful. The code is as follows:
The code is as follows:
I have to say that a big pitfall encountered here is the two ghost things (dot and dot). Under every folder in the operating system, there will be . and.., They represent the current directory and the superior directory of the current directory. What's terrible is that it was not displayed when reading the directory, causing the recursive function to become an infinite loop, because . and .. are at the front of each directory and must be read first. If they are not filtered, they will first read ., which represents this directory, and then recursively enter this directory... These two are the default ones under the operating system, and they are the connectors between this directory and the upper-level directory.
By calculating the size of the directory and deleting the code for non-empty directories, it is very easy to write copy and cut directories. Very similar recursive ideas require the use of the copy file function copy and the file movement function rename. This is quite interesting, rename , literally rename, but doesn’t renaming it to another directory mean cutting it? -_-
3. File reading and writing
Some file reading operations in PHP are very similar to C language, so it is relatively simple. The steps are to first open the file to get the handle, check for errors, then read and write, then close. Get into the habit of closing after opening and processing. It’s a good habit to remember that if a file in C language is not closed, an error will be reported if it is opened twice. I don’t know if I remember correctly, so strict programs have a lot of processing, such as first verifying that the file exists, and then verifying that it is readable. Writability, then close it first, and then open it again. When you open it, you have to check whether it was opened correctly... When opening a file, you must choose the mode to open the file, which determines whether we read or write the file. , of course, is useful for functions that require such operations.
Write files. There are only a few file writing functions: fwrite, fputs, and file_put_contents. Among them, fwrite has the same effect as fputs. file_put_contents writes some content to the file at one time. It does not need to specify the open mode. At the same time, it can also be appended. Or overwrite existing file content, such as:
The code is as follows:
Read files. There are many functions for reading files, including fread (read specified bytes), fgetc (read one), fgets (read one line), file (read all, allocated to an array by line) (return in), file_get_contents (read all returned strings by default), readfile (directly output the contents of the file to the cache, the effect is to output directly on the browser), as fread, fget, fgets run, the file pointer will automatically go to Go later. Therefore, continuous reading is best controlled by loop. What to do when the end of the file is reached? The EOF flag indicates that the end of the file has been reached. It is best to use feof to detect whether the end of the file has been reached. Without further ado, let’s look at the code:
The code is as follows:
If you want a more flexible reading method, you must use fseek and rewind. They can move the file pointer to a specific position. fseek is very flexible and can be moved directly to the beginning or end, or moved forward or backward from the current position. , read the desired content, ftell can also inform the current location, such as:
The code is as follows:
For example, I am now using this method to read a text file written from a to z and see the effect:
The above is all about PHP directory file operations. It is also a personal understanding record. I hope it will be helpful to everyone