search
Homephp教程php手册php文件打开、创建、读取fopen函数的使用说明

php文件打开、创建、读取fopen函数的使用说明

Jun 13, 2016 am 11:15 AM
fopenphpintroduceInstructions for usefunctioncreateOpenoperatedocumentarticleofSimpleread

本文章来介绍一个简单的文件操作函数fopen,fopen函数可以打开,读取,并且协助文件保存,下面我来给大家总结一下php文件的操作。  

打开文件

fopen 最简单语法如下:

fopen(filepath,mode)

下面是打开一个文件的 PHP 代码示例:

 代码如下 复制代码

$f = fopen("c:datainfo.txt", "r");

?>

其中,c:datainfo.txt 是文件路径,r 表示打开文件的模式(mode) 为只读 (read only) 模式。

 

讲述使用 PHP 内置函数 fclose 关闭一个文件。

fclose 函数语法如下:

fclose(filepointer)

如果成功,fclose 函数返回 TRUE,如果失败,fclose 函数返回 FALSE。

下面是一个 fclose 函数的 PHP 代码示例:

 代码如下 复制代码

$f = fopen("c:datainfo.txt", "r");
fclose($f);
?>

fwrite 写入文件


fwrite 函数。

PHP 内置函数 fwrite 用于写入文件。

fwrite 函数的常用语法为:

fwrite(handle,string)
其中,参数 handle 表示文件指针资源 (通常由 fopen 函数创建),string 表示要写入的内容。

下面一个PHP 代码示例演示如何创建一个新文件,并写入内容,然后保存并关闭文件:



$f= fopen("C:blablaphpwrite.txt","w");
fwrite($f,"It is awesome.");
fclose($f);echo "done";
?>

 代码如下 复制代码

 

执行该 PHP 文件后,会创建一个路径为 C:blablaphpwrite.txt 的文件,文件的内容是It is awesome.。

如果你想在现有文件上再追加内容,你只要修改 fopen 的 参数 mode 值即可,如下:

 代码如下 复制代码

$f= fopen("C:blablaphpwrite.txt","a");

有关 fopen 函数的参数 mode 值,详见 fopen。

fwrite 函数返回写入文件的字节数 (number of bytes) ,如果出错,返回 FALSE。


fgets 读取文件一行内容


用 PHP 内置函数 fgets 可以读取文件的一行内容。

fgets读取文件一行内容的语法是:

fgets(filepointer)

下面我们举个例子讲述如何一行行读取一个文件。

假设我们有一个 sites.txt 文件,该文件有三行,内容如下:

woyouxian.comblabla.cngoogle.com
sites.txt 的文件路径是:

C:blablaphpsites.txt
我们用 PHP 一行行读取文件内容,PHP代码如下:

$f= fopen("C:blablaphpsites.txt","r");
while (!feof($f))

$line = fgets($f); 
echo "site: ",$line,"
";
}fclose($f);
?>

 代码如下 复制代码
 

执行该 PHP 文件,返回的显示结果是:

site: woyouxian.comsite: blabla.cnsite: google.com

该 PHP 代码的第一行是打开文件,最后一行是关闭一个文件。当中的 while 循环语句表示,当文件没有结束,就读取一行,循环执行,直到文件指针到文章末尾为止。

feof 函数是PHP 的一个内置函数,用来测试文件指针是否已经到了文件末尾。如果是返回 TRUE,如果不是,返回 FALSE。eof 的英文意思就是 end of file,很容易记。

正常情况下,fgets 函数的返回值是一个字符串,如果出错,返回 FALSE


Statement
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 AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment