Roger McCoy is a developer who has used multiple programming languages including C, Java, JavaScript, Perl, PHP, and Microsoft Visual Basic. He has five years of PHP application development experience, but he may be more famous as a technician in the call center industry.
Let's calculate the number of methods
One of the fun of dealing with modern programming languages such as PHP is that there are plenty of options available. PHP can easily win Perl's motto "There's more than one way to do it" (not only one way to do it), especially in file processing. But among the many available options, which is the best tool for job completion? Of course, the actual answer depends on the object to be parsed, so it is worth the time to explore all options.
Traditional fopen Method
The fopen method may be the most familiar to C and C ++ programmers in the past, because if you have used these languages, they are more or less tools you have mastered for many years. For any of these methods, open the file by using the standard fopen (function for reading data) method, and then close the file using fclose, as shown in Listing 1.
Listing 1: open and read files with fgets
$file_handle = fopen("myfile", "r"); while (!feof($file_handle)) { $line = fgets($file_handle); echo $line; } fclose($file_handle);
|
Although most programmers with years of programming experience are familiar with these functions, let me break them down. Perform the following steps effectively:
1. open the file. $ File_handle stores a reference to the file itself.
2. Check whether you have reached the end of the file.
3. Continue to read the file until it reaches the end of the file, while reading and printing each line.
4. close the file.
Remember these steps, and I will review every file function used here.
Fopen
The fopen function creates a connection to the file. The reason why I say "create a connection" is that in addition to opening a file, fopen can also open a URL:
$fh = fopen("http://127.0.0.1/", "r");
|
This line of code creates a connection to the above page and allows you to start reading it like reading a local file.
Note: the "r" used in fopen indicates that the file is opened in read-only mode. Since writing data to a file is not covered in this article, I will not list all other options. However, for cross-platform compatibility, change "r" to "rb ". You will see this example later.
Feof
The feof command checks whether you have read the end of the file and returns True or False. The loop in Listing 1 continues until you reach the end of the file "myfile. Note: If the URL is read and the socket times out because no data is available for reading, feof returns False.
Fclose
Jump forward to the end of Listing 1, and fclose will implement the opposite function: it will close the connection to the file or URL. After this function is executed, you will no longer be able to read any information from a file or socket.
Fgets
When you skip several lines in Listing 1, you get to the core of File Processing: actually reading the file. The fgets function is the preferred weapon for processing the first example. It extracts a row of data from the file and returns it as a string. After that, you can print or process data in other ways. The example in Listing 1 will print the entire file in detail.
If you decide to limit the size of the data block to be processed, you can add a parameter to fgets to limit the maximum length of the row. For example, use the following code to limit the line length to 80 characters:
$string = fgets($file_handle, 81);
|
Recall the terminator at the end of the "" string in C and set the length to a number greater than the actual value. Therefore, if 80 characters are required, 81 is used in the preceding example. This additional character should be added as long as this function is restricted.
Fread
The fgets function is only available for reading multiple files. It is a more common function, because row-by-row parsing usually makes sense. In fact, several other functions can provide similar functions. However, you do not always need to parse lines by line.
Fread is required. The fread function is slightly different from fgets in terms of processing objectives: it tends to read information from binary files (that is, files that do not primarily contain readable human text. Because the concept of "row" is irrelevant to binary files (Logical Data structures are generally not terminated by new lines), you must specify the number of bytes to read.
$fh = fopen("myfile", "rb"); $data = fread($file_handle, 4096);
|
The above Code reads 4,096 bytes (4 KB) of data. Note: fread does not read more than 8,192 bytes (8 KB) no matter how many bytes are specified ).
1