Home > Backend Development > PHP Tutorial > Reading and writing files in PHP_PHP tutorial

Reading and writing files in PHP_PHP tutorial

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2016-07-13 10:55:09
Original
1051 people have browsed it

There are two more practical functions for reading and writing files in php, fopen and fwrite. With these two functions, you can operate files very well. If you need it, you can read the detailed explanation.

1.fopen (create files and open files)

Syntax:

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" opens in read-only mode, pointing the file pointer to the beginning of the file.
"r+" opens in read-write mode and points 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+" opens in read-write 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.
"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+" opens in read-write mode and points 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:

The code is as follows Copy code
代码如下 复制代码
$fp = fopen("test.txt", "r");
?>
$fp = fopen("test.txt", "r");

?>

2.fclose (close file)

Syntax:

fclose(filepointer)

filepointer, the file pointer to be closed. The fclose function returns TRUE if successful and FALSE if failed.

 代码如下 复制代码
$fp = fopen("test.txt", "r");
fclose($fp);
?>
Example:

The code is as follows Copy code
$fp = fopen("test.txt", "r");

fclose($fp);

?>


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

Syntax:

 代码如下 复制代码
$fp = fopen("test.txt", "r");
while(! feof($fp))
{
echo fgets($fp). "
";
}
fclose($fp);
?>
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:

The code is as follows Copy code
$fp = fopen("test.txt", "r");
while(! feof($fp))

{

echo fgets($fp). "
"; }

 代码如下 复制代码
$fp = fopen("test.txt", "r");
if($fp)
{
for($i=1;! feof($fp);$i++)
{
echo "行".$i." : ".fgets($fp). "
";
 }
}
else
{
 echo "打开文件失败";
}
fclose($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:
The code is as follows Copy code
$fp = fopen("test.txt", "r");<🎜> if($fp)<🎜> {<🎜> for($i=1;! feof($fp);$i++)<🎜> {<🎜> echo "OK".$i." : ".fgets($fp). "
"; } } else { echo "Failed to open file"; } fclose($fp); ?>

Suppose the content of test.txt is:

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

Line 1: hello world
Line 2: hello cnblogs
Line 3: hello heihaozi
Line 4: hello everyone5.fwrite (write 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:

$count+=$flag; }
The code is as follows
 代码如下 复制代码
$fp = fopen("test.txt", "w");//文件被清空后再写入
if($fp)
{
$count=0;
for($i=1;$i<=5;$i++)
{
$flag=fwrite($fp,"行".$i." : "."Hello World!rn");
if(!$flag)
{
echo "写入文件失败
";
   break;
  }
  $count+=$flag;
 }
 echo "共写入".$count."个字符";
}
else
{
 echo "打开文件失败";
}
fclose($fp);
?>
Copy code

$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;

}
echo "A total of ".$count." characters written"; }

else

{ } fclose($fp); ?> The output result of the page is: Write a total of 100 characters 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, the optional parameters of some functions are not listed. http://www.bkjia.com/PHPjc/632264.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/632264.htmlTechArticleThere are two more practical functions for reading and writing files in php, fopen and fwrite functions. It is good to have both of them. The file has been operated. If necessary, you can read the detailed explanation. 1.fopen (create file...
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
Latest Issues
php data acquisition?
From 1970-01-01 08:00:00
0
0
0
PHP extension intl
From 1970-01-01 08:00:00
0
0
0
How to learn php well
From 1970-01-01 08:00:00
0
0
0
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template