Home Backend Development PHP Tutorial PHP中读写文件实现代码_php技巧

PHP中读写文件实现代码_php技巧

May 17, 2016 am 09:15 AM
Read and write files

在PHP中读写文件,可以用到一下内置函数:

1.fopen(创建文件和打开文件)
语法:

复制代码 代码如下:
fopen(filename,mode)

filename,规定要打开的文件。mode,打开文件的模式,可能的值见下表。

mode 说明

"r" 只读方式打开,将文件指针指向文件开头。
"r+" 读写方式打开,将文件指针指向文件开头。
"w" 写入方式打开,将文件指针指向文件开头并将文件大小截为零。如果文件不存在则尝试创建。
"w+" 读写方式打开,将文件指针指向文件开头并将文件大小截为零。如果文件不存在则尝试创建。
"a" 写入方式打开,将文件指针指向文件末尾。如果文件不存在则尝试创建。
"a+" 读写方式打开,将文件指针指向文件末尾。如果文件不存在则尝试创建。
如果成功打开文件,fopen函数的返回值是一个文件指针,如果出错,返回 FALSE。

示例:
复制代码 代码如下:

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

2.fclose(关闭文件)
语法:

fclose(filepointer)
filepointer,要关闭的文件指针。如果成功,fclose 函数返回 TRUE,如果失败,fclose 函数返回 FALSE。
示例:
复制代码 代码如下:

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

3.feof(检测是否已到达文件末尾)
语法:

feof(filepointer)
filepointer,要检测的文件指针,该指针必须指向成功打开没有关闭的文件。如果文件指针到了文件末尾或者出错时,feof函数返回 TRUE。
示例:
复制代码 代码如下:

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

4.fgets(从文件指针中读取一行)
语法:

fgets(filepointer)
filepointer,要读取的文件指针。如果成功,从文件中读取一行并返回字符串,如果失败,返回 FALSE。
示例:
复制代码 代码如下:

$fp = fopen("test.txt", "r");
if($fp)
{
for($i=1;! feof($fp);$i++)
{
echo "行".$i." : ".fgets($fp). "
";
}
}
else
{
echo "打开文件失败";
}
fclose($fp);
?>

假设test.txt的内容为:

hello world
hello cnblogs
hello heihaozi
hello everyone
页面输出的结果为:

行1 : hello world
行2 : hello cnblogs
行3 : hello heihaozi
行4 : hello everyone
5.fwrite(写入文件)
语法:

fwrite(filepointer,string)
filepointer,要写入的文件指针。string,要写入的字符串。如果成功,返回写入的字符数,如果失败,返回 FALSE。
示例:
复制代码 代码如下:

$fp = fopen("test.txt", "w");//文件被清空后再写入
if($fp)
{
$count=0;
for($i=1;$i{
$flag=fwrite($fp,"行".$i." : "."Hello World!\r\n");
if(!$flag)
{
echo "写入文件失败
";
break;
}
$count+=$flag;
}
echo "共写入".$count."个字符";
}
else
{
echo "打开文件失败";
}
fclose($fp);
?>

页面输出的结果为:

共写入100个字符
test.txt文件会被写入:

行1 : Hello World!
行2 : Hello World!
行3 : Hello World!
行4 : Hello World!
行5 : Hello World!
  
注:为了简化操作,部分函数的可选参数没有列出。
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

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

11 Best PHP URL Shortener Scripts (Free and Premium) 11 Best PHP URL Shortener Scripts (Free and Premium) Mar 03, 2025 am 10:49 AM

11 Best PHP URL Shortener Scripts (Free and Premium)

Working with Flash Session Data in Laravel Working with Flash Session Data in Laravel Mar 12, 2025 pm 05:08 PM

Working with Flash Session Data in Laravel

Introduction to the Instagram API Introduction to the Instagram API Mar 02, 2025 am 09:32 AM

Introduction to the Instagram API

Simplified HTTP Response Mocking in Laravel Tests Simplified HTTP Response Mocking in Laravel Tests Mar 12, 2025 pm 05:09 PM

Simplified HTTP Response Mocking in Laravel Tests

Build a React App With a Laravel Back End: Part 2, React Build a React App With a Laravel Back End: Part 2, React Mar 04, 2025 am 09:33 AM

Build a React App With a Laravel Back End: Part 2, React

cURL in PHP: How to Use the PHP cURL Extension in REST APIs cURL in PHP: How to Use the PHP cURL Extension in REST APIs Mar 14, 2025 am 11:42 AM

cURL in PHP: How to Use the PHP cURL Extension in REST APIs

12 Best PHP Chat Scripts on CodeCanyon 12 Best PHP Chat Scripts on CodeCanyon Mar 13, 2025 pm 12:08 PM

12 Best PHP Chat Scripts on CodeCanyon

Notifications in Laravel Notifications in Laravel Mar 04, 2025 am 09:22 AM

Notifications in Laravel

See all articles