The correct way to read files in PHP

Source: Internet
Author: User
Tags socket

Learn how to use PHP's various file functions. View basic file functions such as fopen, Fclose, and feof, and read functions such as fgets, FGETSS, and fscanf. and found a function to process the entire file with one or two lines of code.

Let's count the number of ways

One of the pleasures of dealing with modern programming languages like PHP is that there are a number of options available. PHP can easily win Perl's motto "There ' more than one way to do it" (not only a single method can do this), especially on file processing. But which of the many available options is the best tool to do the job? Of course, the actual answer depends on the goal of parsing the file, so it's worth taking the time to explore all the options.

The traditional method of fopen

The Fopen method may be the most familiar to previous C and C + + programmers, because if you have used these languages, they are more or less the tools that you have mastered for years. For either of these methods, open the file by using the standard method of fopen (the function to read the data), 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 decompose these functions. Effectively perform the following steps:

Open the file. $file _handle stores a reference to the file itself.

Check to see if you have reached the end of the file.

Continue reading the file until you reach the end of the file and print each line while reading.

Closes the file.

With these steps in mind, I'll review each of the file functions used here.

fopen

The fopen function creates a connection to the file. The reason I say "create a connection" is because, in addition to opening the file, fopen can 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 as if you were reading a local file.

Note: the "R" used in fopen will indicate that the file is opened as read-only. Because writing data to a file is not covered in this article, I will not list all the other options. However, if you are reading from a binary file to obtain Cross-platform compatibility, you should change "R" to "RB". You'll see an example of this later.

Feof

The feof command detects whether you have read to 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 more data can be read, feof also returns FALSE.

Fclose

Skip forward to the end of Listing 1, and fclose will implement the function opposite to fopen: it closes the connection to the file or URL. After you perform this function, you will no longer be able to read any information from a file or socket.

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.