The correct method for PHP to read files

Source: Internet
Author: User

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.
Back to Top
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:
Open the file. $ File_handle stores a reference to the file itself.
Check whether you have reached the end of the file.
Continue to read the file until it reaches the end of the file, while reading and printing each line.
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, you should 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 "\ 0" 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 );
Use binary data
Note: the example of this function uses a parameter slightly different from that of fopen. When processing binary data, always remember to include option B in fopen. If you skip this step®Windows®The system may not be able to process files correctly because they will process new rows in different ways. If Linux®System (or another UNIX®Variant), it may seem irrelevant. However, even if it is not developed for Windows, it will obtain good cross-platform maintainability and be a good habit to be followed.
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 ).
Assuming that the file size does not exceed 8 KB, the following code should be able to read the entire file into a string. $ Fh = fopen ("myfile", "rb ");
$ Data = fread ($ fh, filesize ("myfile "));
Fclose ($ fh );
If the file length is greater than this value, you can only read the remaining content in a loop.
Fscanf
Back to string processing, fscanf also follows the traditional C-file library function. If you are not familiar with it, fscanf will read the field data from the file into the variable. List ($ field1, $ field2, $ field3) = fscanf ($ fh, "% s ");
The format strings used by this function are described in many places (such as PHP.net), so we will not repeat them here. In this case, string formatting is extremely flexible. It is worth noting that all fields are placed in the return value of the function. (In c, they are all passed as parameters .)
Fgetss
The fgetss function is different from the traditional file function and enables you to better understand the power of PHP. This function is similar to the fgets function, but will remove any HTML or PHP Mark found, leaving only plain text. View the following HTML file.
List 2. example HTML file
<Html>
<Head> <title> My title </title> <Body>
<P> If you understand what "Cause there ain't no one for to give you no pain"
Means then you listen to too much of the band America </p>
</Body>
</Html>
Then filter it using the fgetss function.
Listing 3. Using fgetss
$ File_handle = fopen ("myfile", "r ");
While (! Feof ($ file_handle )){
Echo = fgetss ($ file_handle );
}
Fclose ($ file_handle );
The following is the output: My title
If you understand what "Cause there ain't no one for give you no pain"
Means then you listen to too much of the band America
Fpassthru Function
Regardless of how the file is read, you can use fpassthru to dump the remaining data to the standard output channel. Fpassthru ($ fh );
In addition, this function prints data, so you do not need to use variables to obtain data.
Non-linear File Processing: Skip access
Of course, the above functions only allow sequential reading of files. More complex files may require you to jump back and forth to different parts of the file. In this case, fseek is used. Fseek ($ fh, 0 );
The above example will jump back to the beginning of the file. If you do not need to return completely -- we can set to return kilobytes -- then you can write: fseek ($ fh, 1024 );
You have some other options since PHP V4.0. For example, if you want to jump forward 100 bytes from the current position, you can try: fseek ($ fh, 100, SEEK_CUR );
Similarly, you can use the following code to jump back to 100 bytes: fseek ($ fh,-100, SEEK_CUR );
If you need to jump back to the first 100 bytes at the end of the file, use SEEK_END. Fseek ($ fh,-100, SEEK_END );
After arriving at a new location, you can use fgets, fscanf, or any other method to read data.
Note: fseek cannot be used for processing files that reference URLs.
Back to Top
Extract the entire file
Now, we will have access to some of the more unique PHP file processing functions: use one or two lines to process large pieces of data. For example, how to extract a file and display all its content on a Web page? Okay. You can see the example of using loop in fgets. But how can this process be made simpler? Using fgetcontents will make the process super simple. This method will put the entire file into a string. $ My_file = file_get_contents ("myfilename ");
Echo $ my_file;
Although it is not the best practice, you can write this command more concisely as echo file_get_contents ("myfilename ");
This article describes how to process local files, but it is worth noting that you can use these functions to extract, ECHO, and parse other Web pages. Echo file_get_contents ("http: // 127.0.0.1 /");
This command is equivalent to: $ fh = fopen ("http: // 127.0.0.1/", "r ");
Fpassthru ($ fh );
You will certainly view this command and think: "That is too laborious ". PHP developers agree with you. Therefore, you can shorten the preceding command to readfile ("http: // 127.0.0.1 /");
The readfile function dumps all the content of a file or Web page to the default output buffer. By default, if it fails, this command prints the error message. To avoid this behavior (if needed), try: @ readfile ("http: // 127.0.0.1 /");
Of course, if you do need to parse the file, a single string returned by file_get_contents may be a little overkill. Your first response may be to use the split () function to break it down. $ Array = split ("\ n", file_get_contents ("myfile "));
But since there is already a good function for you to perform this operation, why is it so expensive? PHP's file () function completes this operation step by step: it returns a string array that is divided into several rows. $ Array = file ("myfile ");
Note that the two examples are slightly different. Although the split command will delete the new line, when the file command is used (the same as the fgets command), the new line will still be appended to the string in the array.
However, PHP is far more powerful than that. You can use parse_ini_file in a command to parse the. ini file of the entire PHP style. The parse_ini_file command accepts files similar to those shown in Listing 4.
Listing 4. example. ini file
; Comment
[Personal information]
Name = "King Arthur"
Quest = To seek the holy grail
Favorite color = Blue
[More stuff]
Samuel Clemens = Mark Twain
Caryn Johnson = Whoopi Goldberg
The following command dumps the file as an array and prints the array: $ file_array = parse_ini_file ("holy_grail.ini ");
Print_r $ file_array;
The following output is the result:
Listing 5. Output
Array
(
[Name] => King Arthur
[Quest] => To seek the Holy Grail
[Favorite color] => Blue
[Samuel Clemens] => Mark Twain
[Caryn Johnson] => Whoopi Goldberg
)
Of course, you may notice that this command combines all parts. This is the default action, but you can easily modify it by passing the second parameter to parse_ini_file: process_sections, which is a Boolean variable. Set process_sections to True. $ File_array = parse_ini_file ("holy_grail.ini", true );
Print_r $ file_array;
And you will get the following output:
Listing 6. Output
Array
(
[Personal information] => Array
(
[Name] => King Arthur
[Quest] => To seek the Holy Grail
[Favorite color] => Blue
)
[More stuff] => Array
(
[Samuel Clemens] => Mark Twain
[Caryn Johnson] => Whoopi Goldberg
)
)
PHP puts the data in a multi-dimensional array that can be easily parsed.
For PHP file processing, this is just the tip of the iceberg. More complex functions such as tidy_parse_file and xml_parse can help you process HTML and XML documents respectively. For more information about the use of these special functions, see references. If you want to process those types of files, the references are worth looking at, but you don't have to worry too much about each possible file type mentioned in this article, below are some good general rules for processing the functions described so far.
Back to Top
Best practices
Never assume that everything in the program will run as planned. For example, what if the file you want to search for has been moved? What if the permission has been changed and its content cannot be read? You can pre-check these problems by using file_exists and is_readable.
Listing 7. Using file_exists and is_readable
$ Filename = "myfile ";
If (file_exists ($ filename) & is_readable ($ filename )){
$ Fh = fopen ($ filename, "r ");
# Processing
Fclose ($ fh );
}
However, in practice, using such code may be too cumbersome. Processing fopen return values is simpler and more accurate. If ($ fh = fopen ($ filename, "r ")){
# Processing
Fclose ($ fh );
}
If fopen fails, False is returned. This ensures that the file is processed only after the file is opened successfully. Of course, if the file does not exist or is unreadable, you can expect a negative return value. This will enable this check to check all possible problems. In addition, if opening fails, you can exit the program or let the program display an error message.
Like the fopen function, the file_get_contents, file, and readfile functions return False when opening or processing files fails. The fgets, fgetss, fread, fscanf, and fclose functions also return False when an error occurs. Of course, except fclose, you may have processed the return values of these functions. When fclose is used, no operations are performed even if the file processing is not properly closed. Therefore, you do not need to check the return value of fclose.
Back to Top
You can choose
PHP does not lack an effective method for reading and parsing files. Typical functions such as fread may be the best choice most of the time, or you may find yourself more attracted to the simplicity of readfile when readfile meets the needs of the task. It actually depends on the operation to be completed.
To process a large amount of data, fscanf will prove its value and be more efficient than using the split and sprintf commands with file. On the contrary, if you want to echo a large amount of text with only a few modifications, it may be more appropriate to use file, file_get_contents, or readfile. This may be the case when PHP is used for caching or when an appropriate proxy server is created.
PHP provides you with a large number of file processing tools. Learn more about these tools and what tools are most suitable for the projects to be processed. You have many options, so you can use them to process files in PHP.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.