PHP file reading function application example, _ PHP Tutorial

Source: Internet
Author: User
Tags php website
Application instance of the PHP file reading function ,. For PHP file reading applications, php file reading operations involve more php file operation functions than file writing operations. these functions are described in detail in the code instance. Application instance for reading the php file reading function,

Php file read operations involve more php file operation functions than file write operations. these functions are described in detail in the code instance.

The data storage method for reading text files involves the following three steps and some file operation functions:

1. open the file (File operation function: fopen)
2. file data reading (file operation functions: fgets, file, readfile, feof, etc)
3. close the file (File operation function: fclose)

The following uses the php file read/write operation code instance to explain the specific application of the file read method. in the instance, you can call different php file read operation functions to read data in the text file, you can learn more about the function for reading PHP files so that they can be used properly in PHP website development. The data written to a text file comes from the PHP file read/write operation file writing tutorial. for details about the file read/write mode in the fopen function, refer to this article.

Php file read operation code instance

<?$readFun = "fread";switch ($readFun){case "fgetss":@$fp = fopen("leapsoulcn.txt","r") or die("system error");$allowable_tags = "";while (!feof($fp)) {$output = fgetss($fp,100,$allowable_tags);echo $output;}fclose($fp);break;case "fgetcsv":@$fp = fopen("leapsoulcn.txt","r") or die("system error");while (!feof($fp)) {$output = fgetcsv($fp,100,"\t");print_r($output);}fclose($fp);break;case "readfile":echo readfile("leapsoulcn.txt");break;case "fpassthru":@$fp = fopen("leapsoulcn.txt","r") or die("system error");if(!fpassthru($fp))exit();fclose($fp);break;case "file":$output = file("leapsoulcn.txt");print_r($output);break;case "fgetc":@$fp = fopen("leapsoulcn.txt","r") or die("system error");while (!feof($fp)) {$str = fgetc($fp);echo ($str == "\n"?"
":$str);}fclose($fp);break;case "fread":@$fp = fopen("leapsoulcn.txt","r") or die("system error");echo fread($fp,300);fclose($fp);break;default:@$fp = fopen("leapsoulcn.txt","r") or die("system error");while (!feof($fp)) {$output = fgets($fp,100);echo $output;}fclose($fp);break;}?>

Note: In the above example, you can assign a value to $ readFun to call different methods for reading php files, the involved php file read operation functions include fgets, fgetss, fgetcsv, readfile, fpassthru, file, and fgetc.

Differences between functions fgets, fgetss, and fgetcsv for PHP file reading

In code instances, the default PHP file read operation function is fgets. the fgetss and fgetcsv functions are the same as fgets functions. they read one row of the file at a time until the end of the file. Here, I set the data length in the read text file to 100, that is, the maximum read length is 99 (100-1, when the linefeed \ n or the file terminator EOF or 99 bytes are read from the file, data reading is stopped. The fgets function returns the data read by the file, which is a string type.

The fgetss function is a variant of the fgets function. it can strip PHP and HTML tags and pass a third parameter to filter unnecessary data, which improves website security, for example, the message book can filter user input data. the fgetss function prototype is as follows:

String fgetss (resource fp, int length, string [optional] allowable_tags)

Allowable_tags.txt is optional. In this example, a line of text containing the html, body, and h1 mark is written in the leapsoulcn.txt file. Then, in the code, I set that only the h1 mark can appear.

The fgetcsv function is another variant of fgets. The difference is that when the data written to your text file uses a delimiter, you can use fgetcsv to split a row into multiple rows, the returned results are stored in the array. the Function prototype is as follows:

Array fgetcsv (resource fp, int length, string [optional] delimiter, string [optional] enclosure)

Delimiter is optional. because I used \ t in the data written to the file, I used \ t in the fgetcsv function for file reading in the instance, then print out the array structure returned by fgetcsv through print_r.

The three php file read operation functions fgets, fgetss, and fgetcsv all need to use the fopen function to open the file to be read in advance, and use the feof function to determine whether the file pointer has reached the end of the file, remember to use the fclose function to close the file after the read operation is complete.

Fgetc: read a single character
The fgetc function is used to read a character. in the code example, I read the character one by one. when the \ n character is encountered, it is converted into the br Mark in the html file, in order to display the specific line feed effect in the browser, of course, the efficiency of this function is certainly relatively low, not recommended.

Differences between the readfile, fpassthru, and file functions for PHP file reading

In common, the three functions can read the entire file at a time, rather than one row or character at a time. The difference is:
The readfile function opens the file and returns the file content directly to the browser. like the fopen function, the function returns the total number of characters in the file. The second parameter of the readfile function is optional, specifies whether PHP should search for files in include_path. In code instances, the echo statement is not used to output the read file content, but to output the total number of characters read from the file. the read file content readfile function has been automatically output, which must be clear! The readfile function is prototype as follows:
Int readfile (string filename, int [optional] use_include_path)

The file function is another method for reading files. it sends the content of the read files to an array with one array unit per row. The file function is prototype as follows:
Array file (string filename, bool [optional] use_include_path)

The fpassthru () function is used to output all the remaining data at the file pointer. that is, if the file pointer is not at the beginning, it only outputs the data after the file pointer. This function reads the given file pointer from the current position to EOF and writes the result to the output buffer. the returned value is the number of output characters. If an error occurs, FALSE is returned. Compared with the readfile () function, the fpassthru () function must first open the file and close the file after the data is read.

Fread, file_exists, and filesize functions

Fread function is also a method for reading files. it can read any byte from the file, either the length or the end of the file. The read function prototype is as follows:

String fread (resource fp, int length)

When you use the fread function, the filesize function can solve this problem when you want to read all the data in the file and do not know the length of the file data. that is

<?  @$fp = fopen("leapsoulcn.txt","r") or die("system error");  echo fread($fp,filesize("leapsoulcn.txt"));  fclose($fp);?>

In the PHP file read/write tutorial, we have not used the file_exists function. generally, for various considerations, sometimes when the file does not exist, we do not like creating a new file, in this case, we need to use the file_exists function before using the fopen function to determine whether the file exists, that is

<? If (file_exists ("leapsoulcn.txt") {// read and write php files}?>

The above describes various methods for PHP to read file content. by properly applying the PHP file read/write operation function, you can implement simple message books, website log records, and other functions.

The PHP file read operation involves more php file operation functions than the file write operation. these functions are described in detail in the code instance. Read...

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.