PHP to read and write files to implement code _php skills

Source: Internet
Author: User
To read and write files in PHP, you can use the built-in functions:

1.fopen (create file and open file)
Grammar:
Copy Code code as follows:
fopen (Filename,mode)

filename, specify the file to open. mode, open the pattern of the file, the possible values are shown in the following table.

Mode description

"R" opens in read-only mode, pointing the file pointer at the beginning of the file.
The "r+" read-write mode opens, pointing the file pointer at the beginning of the file.
The "W" write mode opens, pointing the file pointer at the beginning of the file and truncating the file size to zero. Try to create if the file does not exist.
The "w+" read-write mode opens, pointing the file pointer at the beginning of the file and truncating the file size to zero. Try to create if the file does not exist.
The "A" write is opened, pointing the file pointer at the end of the file. Try to create if the file does not exist.
A + read-write mode opens, pointing the file pointer to the end of the file. Try to create if the file does not exist.
If the file is successfully opened, the return value of the fopen function is a file pointer and, if an error occurs, returns FALSE.

Example:
Copy Code code as follows:

<?php
$fp = fopen ("Test.txt", "R");
?>

2.fclose (Close file)
Grammar:

Fclose (Filepointer)
Filepointer, the file pointer to close. If successful, the Fclose function returns TRUE, and if it fails, the Fclose function returns FALSE.
Example:
Copy Code code as follows:

<?php
$fp = fopen ("Test.txt", "R");
Fclose ($FP);
?>

3.feof (detects whether the end of the file has been reached)
Grammar:

Feof (Filepointer)
Filepointer, the file pointer to be instrumented, which must point to a successful open file that is not closed. The feof function returns TRUE if the file pointer is at the end of the file or if an error occurs.
Example:
Copy Code code as follows:

<?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:
Copy Code code as follows:

<?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
5.fwrite (write file)
Grammar:

Fwrite (filepointer,string)
Filepointer, the file pointer to write to. String that is to be written. If successful, returns the number of characters written and, if it fails, returns FALSE.
Example:
Copy Code code as follows:

<?php
$fp = fopen ("Test.txt", "w");//file is emptied and then written
if ($FP)
{
$count = 0;
For ($i =1 $i <=5; $i + +)
{
$flag =fwrite ($fp, line. $i. ":". " Hello world!\r\n ");
if (! $flag)
{
echo "Write file failed <br>";
Break
}
$count + + $flag;
}
echo "Altogether writes". $count. " Characters ";
}
Else
{
echo "Open file failed";
}
Fclose ($FP);
?>

The results of the page output are:

100 characters written in a total
Test.txt files will be written to:

Line 1:hello world!
Line 2:hello world!
Line 3:hello world!
Line 4:hello world!
Line 5:hello world!
  
Note: The optional parameters for some functions are not listed to simplify the operation.

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.