Home > Backend Development > PHP Tutorial > Code for reading and writing files in PHP_PHP tutorial

Code for reading and writing files in PHP_PHP tutorial

WBOY
Release: 2016-07-21 15:24:23
Original
910 people have browsed it

To read and write files in PHP, you can use the following built-in functions:

1.fopen (create files and open files)
Syntax:

Copy code The code is as follows:
fopen(filename,mode)

filename specifies the file to be opened. mode, the mode for opening the file. Possible values ​​are shown in the table below.

mode Description

"r" Open in read-only mode, pointing the file pointer to the beginning of the file.
"r+" Open in reading and writing mode and point the file pointer to the beginning of the file.
"w" turns on writing mode, points the file pointer to the beginning of the file and truncates the file size to zero. If the file does not exist then attempts to create it.
"w+" Open in reading and writing mode, point the file pointer to the beginning of the file and truncate the file size to zero. If the file does not exist then attempts to create it.
"a" opens in writing mode and points the file pointer to the end of the file. If the file does not exist then attempts to create it.
"a+" Open in read-write mode and point the file pointer to the end of the file. If the file does not exist then attempts to create it.
If the file is successfully opened, the return value of the fopen function is a file pointer. If an error occurs, FALSE is returned.

Example:
Copy code The code is as follows:

$fp = fopen("test.txt", "r");
?>

2.fclose (close the file)
Syntax:

fclose(filepointer)
filepointer, the file pointer to be closed. The fclose function returns TRUE if successful and FALSE if failed.
Example:
Copy code The code is as follows:

$fp = fopen(" test.txt", "r");
fclose($fp);
?>

3.feof (check whether the end of the file has been reached)
Syntax:

feof(filepointer)
filepointer, the file pointer to be detected, which must point to a file that was successfully opened but not closed. If the file pointer reaches the end of the file or an error occurs, the feof function returns TRUE.
Example:
Copy code The code is as follows:

$fp = fopen(" test.txt", "r");
while(! feof($fp))
{
echo fgets($fp). "
";
}
fclose($fp);
?>

4.fgets (read a line from the file pointer)
Syntax:

fgets(filepointer)
filepointer, the file pointer to be read. Reads a line from the file and returns a string if successful, or FALSE if failed.
Example:
Copy code The code is as follows:

$fp = fopen(" test.txt", "r");
if($fp)
{
for($i=1;! feof($fp);$i++)
{
echo "line".$i." : ".fgets($fp). "
";
}
}
else
{
echo "Failed to open file ";
}
fclose($fp);
?>

Suppose the content of test.txt is:

hello world
hello The output result of the cnblogs
hello heihaozi
hello everyone
page is:

Line 1: hello world
Line 2: hello cnblogs
Line 3: hello heihaozi
Line 4: hello everyone
5.fwrite (write to file)
Syntax:

fwrite(filepointer, string)
filepointer, the file pointer to be written. string, the string to be written. Returns the number of characters written if successful, or FALSE if failed.
Example:
Copy code The code is as follows:

$fp = fopen(" test.txt", "w");//The file is cleared and then written
if($fp)
{
$count=0;
for($i=1;$ i<=5;$i++)
{
$flag=fwrite($fp,"OK".$i." : "."Hello World!rn");
if(!$flag )
{
echo "Failed to write file
";
break;
}
$count+=$flag;
}
echo "Total writes ".$count." characters";
}
else
{
echo "Failed to open file";
}
fclose($fp);
?> ;

The page output result is:

A total of 100 characters will be written
The test.txt file will be written:

Line 1: Hello World!
Line 2 : Hello World!
Line 3: Hello World!
Line 4: Hello World!
Line 5: Hello World!
 
Note: In order to simplify the operation, some optional parameters of the function are not included List.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/324337.htmlTechArticleTo read and write files in PHP, you can use the following built-in functions: 1.fopen (create files and open files) Syntax: Copy the code as follows: fopen(filename,mode) filename, stipulates to open...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template