PHP file read-write instance program

Source: Internet
Author: User

How PHP reads files

The file_get_contents () function reads the entire file into a string.

Example

The code is as follows Copy Code

<?php
Echo file_get_contents ("test.txt");
?>

Output:

This was a test file with test text.

After PHP has opened the file, you need to read the file, typically using the fgets () function.

The function can read one row at a time from the file, reading the data continuously, to a newline character that encounters the line, or to the full text's ending symbol EOF.

The fgets () function can only read one row of data, so if you need to read all the data for a file, use a loop statement to complete it. Like what:

Example:
The code is as follows:

The code is as follows Copy Code
<?php
$fp = fopen ("Test.txt", "R");
while (! feof ($FP))
{
Echo fgets ($FP). "<br/>";
}
Fclose ($FP);
?>

4.fgets (read one row from the file pointer)
Grammar:

Fgets (Filepointer)
Filepointer, the file pointer to read. If successful, reads a row from the file and returns a string, FALSE if it fails.
Example:

The code is as follows:

The code is as follows Copy Code
<?php
$fp = fopen ("Test.txt", "R");
if ($FP)
{
For ($i =1;! feof ($fp); $i + +)
{
echo "line". $i. ":". Fgets ($FP). " <br/> ";
}
}
Else
{
echo "Open file failed";
}
Fclose ($FP);
?>

Suppose the contents of the Test.txt are:

Hello World
Hello Cnblogs
Hello Heihaozi
Hello everyone
The results of the page output are:

Line 1:hello World
Line 2:hello Cnblogs
Line 3:hello Heihaozi
Line 4:hello Everyone

where the feof () function is used to detect the end of a file. The only argument for this function is the file pointer (that is, the $fp file).

Of course, in PHP you can also use the ReadFile () function to read the entire file at once. This function includes opening the file, reading the file and outputting it to the browser and

Closes the file. Like what:

The code is as follows Copy Code


<?php
$bruce =readfile ("http://www.111cn.net");
Echo $bruce;
?>


3,php How to close a file

You can close a file by using the function fclose ().

Second, how PHP writes data to the file

As with PHP reading files, PHP writes to files as well: Open files, write data, and close files. The method for opening and closing a file is described above.

What about writing data to a file in PHP?

Use the fwrite () function, such as fwrite (file path, write content):

The code is as follows Copy Code
<?php
$bruce =fopen ("http://www.111cn.net/", "R");
if (! $bruce)
{
Echo ' file does not exist ';
Exit
}
while (!feof ($bruce))
{
$rose =fgets ($bruce);
$james =fopen ("index.htm", "a");
Fwrite ($james, $rose);
Fclose ($james);
}
Fclose ($bruce);
?>

<a href= "index.htm" > generates local files for 111cn.net content </a>

 
understands PHP's read and write files, and saves the simplest data saved in the text

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.