PHP Read file content several correct methods

Source: Internet
Author: User
Tags php read file

method to read with a while to some fgets line
$file _name= "1.txt";

The code is as follows Copy Code

$FP =fopen ($file _name, ' R ');

while (!feof ($FP))
{
$buffer =fgets ($fp, 4096);
echo $buffer. " <br> ";
}
Fclose ($FP);



Method two uses file to save to an array at once and then output with foreach

The code is as follows Copy Code
$array = file ($file);
foreach ($array as $v =>$_v)
{
echo $_v, ' <br/> ';
}


Method three uses file_get_contents once to read out

The code is as follows Copy Code
if (Is_file ($file _name))
{
$CN = file_get_contents ($file _name);
Echo $CN;
}


/*
Correlation Function Reference Address
fopen http://www.111cn.net/phper/18/d5da4dcc30303f7684a91dec184e24fc.htm
Whether the feof to the end of the file
Fgets http://www.111cn.net/phper/21/php-fgets.htm
Fclose off
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.