PHP 文件处理
来源:互联网 发布:猎豹网校python百度云 编辑:程序博客网 时间:2024/05/21 08:55
fopen() 函数用于在 PHP 中打开文件。
打开文件
fopen() 函数用于在 PHP 中打开文件。
此函数的第一个参数含有要打开的文件的名称,第二个参数规定了使用哪种模式来打开文件:
<html><body><?php$file=fopen("welcome.txt","r");?></body></html>
文件可能通过下列模式来打开:
读/写。创建新文件。如果文件已存在,则返回 FALSE 和一个错误。
注释:如果 fopen() 无法打开指定文件,则返回 0 (false)。
例子
如果 fopen() 不能打开指定的文件,下面的例子会生成一段消息:
<html><body><?php$file=fopen("welcome.txt","r") or exit("Unable to open file!");?></body></html>
关闭文件
fclose() 函数用于关闭打开的文件。
<?php$file = fopen("test.txt","r");//some code to be executedfclose($file);?>
检测 End-of-file
feof() 函数检测是否已达到文件的末端 (EOF)。
在循环遍历未知长度的数据时,feof() 函数很有用。
注释:在 w 、a 以及 x 模式,您无法读取打开的文件!
if (feof($file)) echo "End of file";
逐行读取文件
fgets() 函数用于从文件中逐行读取文件。
注释:在调用该函数之后,文件指针会移动到下一行。
例子
下面的例子逐行读取文件,直到文件末端为止:
<?php$file = fopen("welcome.txt", "r") or exit("Unable to open file!");//Output a line of the file until the end is reachedwhile(!feof($file)) { echo fgets($file). "<br />"; }fclose($file);?>
逐字符读取文件
fgetc() 函数用于从文件逐字符地读取文件。
注释:在调用该函数之后,文件指针会移动到下一个字符。
例子
下面的例子逐字符地读取文件,直到文件末端为止:
<?php$file=fopen("welcome.txt","r") or exit("Unable to open file!");while (!feof($file)) { echo fgetc($file); }fclose($file);?>
PHP Filesystem 参考手册
如需完整的 PHP 文件系统参考手册,请访问 W3School 提供的 PHP Filesystem 参考手册。
- [PHP]文件上传处理
- php文件处理
- 文件处理 php
- php处理上传文件
- PHP 简单文件处理
- PHP 文件处理
- PHP 文件处理
- 【php】文件处理操作
- php文件处理
- php文件的处理
- PHP上传文件处理
- PHP 处理 TXT 文件
- php 文件处理
- php 文件处理
- php文件处理
- PHP 文件处理
- PHP 文件处理
- php上传文件处理
- PHP $_GET
- PHP $_POST
- PHP Date()
- HTML 标签
- PHP 引用文件
- PHP 文件处理
- PHP 文件上传
- PHP Cookies
- PHP Sessions
- PHP 发送电子邮件
- PHP 安全的电子邮件
- PHP 错误处理
- PHP 异常处理
- PHP 过滤器(Filter)