Three, read the file

Source: Internet
Author: User
Tags fread

1, the Read file function has

(1) fread (file name, read size), such as Fread ($file _path,filesize ($file _path));

(2) file_get_contents (file name, this function actually contains fopen (file) +fread (file) +fclose (file));

, 2, there are three ways to read files

The first way to read files:

/* The first way to read the file */$file _path = "Test.txt", if (file_exists ($file _path)) {    //Open file    $fp = fopen ($file _path, "A +");    Read the content and enter    $conn = fread ($fp, FileSize ($file _path));    echo "The contents of the file is:<br/>";    The newline character in the Web page is <br/> the line break in the text is \ r \ n, you need to replace the newline character    $conn = Str_replace ("\ r \ n", "<br/>", $conn);    echo $conn;    Fclose ($file _path);} else{    echo ' file does not exist! ‘;}

The second way of reading files:

/* The second way to read the file */$file _path = "Test.txt"; $conn = file_get_contents ($file _path); $conn = Str_replace ("\ r \ n", "<br/>", $conn); Echo $conn;

The third way to read files:

/* There is a flaw in the first two ways, if the read file is too large, it is prone to error, so the third Way is recommended *//* the third method of reading the file */$file _path = "Test.txt"; if (file_exists ($file _path)) {    $fp = fopen ($file _path, "A +");    $buffer = 1024x768;    $str = "";    while (!feof ($FB)) {        $str. = Fread ($fp, $buffer);    }     $str = Str_replace ("\ r \ n", "<br/>", $str);    echo $str;    Fclose ($file _path);} else{    echo "file does not exist! ";}

  

Three, read the file

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.