Detailed explanation of a series of methods for reading php files

怪我咯
Release: 2023-03-13 13:44:02
Original
2549 people have browsed it

In the previous articlePHP Tutorial, we introduced how to use PHP to implement the file writing function in file reading and writing operations. Next, we will share with you an application example of the PHP file reading function. PHP file reading operations involve more PHP file operation functions than file writing operations. These functions will be introduced in detail in the code examples.

The method of reading data stored in text files mainly involves three steps and some file operation functions as follows:

1. Open the file (file operation function: fopen)

2. Read file data (file operation functions: fgets, file, readfile, feof, etc.)

3. Close the file (file operation function: fclose)

The following still uses PHP file reading and writing operation codeExample explanationThe specific application of the file reading method. In the example, by calling different PHP file reading operation functions to read the data in the text file, you can deepen the PHP file reading Get an understanding of operation functions so that they can be reasonably applied in PHP website development. The data written in the text file comes from the file writing tutorial of PHP file reading and writing operations. You can also refer to this article for the file reading and writing mode in the fopen function.

PHP file reading operation code example

<?php
    $readFun = "fread";
    switch ($readFun)
    {
        case "fgetss":
            @$fp = fopen("leapsoulcn.txt","r") or die("system error");
            $allowable_tags = "<h1>";
            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"?"<br/>":$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;
    }
?>
Copy after login

Comments: In the above example, you can achieve it by assigning a value to $readFun When calling different PHP file reading methods, the PHP file reading operation functions involved include fgets, fgetss, fgetcsv, readfile, fpassthru, file, fgetc and other functions.

In the code example, the default PHP file reading operation function is fgets. The functions of fgetss and fgetcsv functions are the same as fgets. They both read one line in the file at a time. until the end of the file. Here I set the data length in the text file to be read to 100, that is, the maximum read length is 99 (100-1). In this way, when the newline character \n or the end of file character EOF is encountered or the reading from the file is completed, Stop reading data when 99 bytes are reached. The fgets function returns the data read from the file, in string type.

fgetc: Read a single character

The fgetc function is used to read a character. In the code example, I read the characters one by one. When I encounter \n Characters are converted into the br tag in the HTML file so that the specific line break effect can be displayed in the browser. Of course, the efficiency of this function is definitely relatively low and is not recommended.

The above is the detailed content of Detailed explanation of a series of methods for reading php files. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template