Php: Several correct methods for reading file content-PHP source code

Source: Internet
Author: User
Ec(2); use whileto read file_name&quot=1.txt & quot; & nbsp; the Code is as follows: copy the code $ fpfopen ($ file_name, r); & nbsp; while (! Feof ($ fp) & nbsp ;{& nbsp; $ bufferfgets ($ fp, 4096); & nbsp; e script ec (2); script

// Method 1 use while to read some fgets rows
$ File_name = "1.txt ";

The Code is as follows:

$ Fp = fopen ($ file_name, 'R ');

While (! Feof ($ fp ))
{
$ Buffer = fgets ($ fp, 4096 );
Echo $ buffer ."
";
}
Fclose ($ fp );



// Method 2: Save the file to the array once and use foreach to output

The Code is as follows:
$ Array = file ($ file );
Foreach ($ array as $ v =>$ _ v)
{
Echo $ _ v ,'
';
}


// Method 3: Read data at a time using file_get_contents

The Code is as follows:
If (is_file ($ file_name ))
{
$ Cn = file_get_contents ($ file_name );
Echo $ cn;
}


/*
Related function reference address
Fopen http://www.111cn.net/phper/18/d5da4dcc30303f7684a91dec184e24fc.htm
Whether feof is to the end of the file
Http://www.111cn.net/phper/21/php-fgets.htm fgets
Close fclose
File http://www.111cn.net/phper/24/f5d9e092cfa826b89c45b339bdd3dd4f.htm
File_get_contents http://www.111cn.net/phper/24/f8d52eaae81ea27383375ead36ffbd4d.htm
Foreach http://www.111cn.net/phper/18/foreach-foreach.htm
*/

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.