21xrx.com
2025-03-13 23:27:10 Thursday
登录
文章检索 我的文章 写文章
PHP文件打开/读/关闭
2021-07-22 15:22:01 深夜i     --     --
P H P / /


在本章中,我们将教您在服务器上如何打开,读取和关闭文件 。


PHP打开文件 - fopen()

更好的打开文件的方法是使用fopen()函数。 此函数比readfile()函数提供更多选项。

我们将在课程中使用文本文件“webdictionary.txt”:

AJAX = Asynchronous JavaScript and XML
CSS = Cascading Style Sheets
HTML = Hyper Text Markup Language
PHP = PHP Hypertext Preprocessor
SQL = Structured Query Language
SVG = Scalable Vector Graphics
XML = EXtensible Markup Language

fopen()的第一个参数包含要打开的文件的名称和 第二个参数指定文件应打开的模式。 以下例子 如果fopen()函数无法打开指定的文件,还会生成错误:

<?php
$myfile = fopen("webdictionary.txt", "r") or die("Unable to open file!");
echo fread($myfile,filesize("webdictionary.txt"));
fclose($myfile);
?>

 

提示:fread()和fclose()函数的解释如下。

可以以下模式之一打开该文件:

Modes Description
r Open a file for read only. File pointer starts at the beginning of the file
w Open a file for write only. Erases the contents of the file or creates a new file if it doesn't exist. File pointer starts at the beginning of the file
a Open a file for write only. The existing data in file is preserved. File pointer starts at the end of the file. Creates a new file if the file doesn't exist
x Creates a new file for write only. Returns FALSE and an error if file already exists
r+ Open a file for read/write. File pointer starts at the beginning of the file
w+ Open a file for read/write. Erases the contents of the file or creates a new file if it doesn't exist. File pointer starts at the beginning of the file
a+ Open a file for read/write. The existing data in file is preserved. File pointer starts at the end of the file. Creates a new file if the file doesn't exist
x+ Creates a new file for read/write. Returns FALSE and an error if file already exists

 


PHP读取文件 - fread()

fread()函数打开文件读取。

fread()的第一个参数是读取的文件的名称 第二个参数指定要读取的最大字节数。

以下PHP代码将“WebDictionary.txt”文件读取到末尾:

fread($myfile,filesize("webdictionary.txt"));

PHP关闭文件 - fclose()

fclose()函数用于关闭打开的文件。

在完成使用后关闭所有文件是一个很好的编程习惯。

fclose()需要文件的名称(或包含的变量 文件名)我们要关闭:

<?php
$myfile = fopen("webdictionary.txt", "r");
// some code to be executed....
fclose($myfile);
?>

PHP读取单行 - fgets()

fgets()函数用于从文件中读取单行。

下面的示例输出“WebDictionary.txt”文件的第一行:

<?php
$myfile = fopen("webdictionary.txt", "r") or die("Unable to open file!");
echo fgets($myfile);
fclose($myfile);
?>

 

注意:调用fgets()函数后,文件指针已移动到下一行。


PHP检查文件末尾 - feof()

feof()函数是否已达到“文件末尾”(EOF)。

feof()函数用于判断是否到达文件末尾。

下面的示例按行读取“WebDictionary.txt”文件,直到达到文件末尾:

<?php
$myfile = fopen("webdictionary.txt", "r") or die("Unable to open file!");
// Output one line until end-of-file
while(!feof($myfile)) {
  echo fgets($myfile) . "<br>";
}
fclose($myfile);
?>

 


PHP读取单个字符 - fgetc()

fgetc()函数用于从文件中读取单个字符。

下面的示例读取“webdictionary.txt”文件字符,直到文件末尾的字符:

<?php
$myfile = fopen("webdictionary.txt", "r") or die("Unable to open file!");
// Output one character until end-of-file
while(!feof($myfile)) {
  echo fgetc($myfile);
}
fclose($myfile);
?>

 

注意:调用fgetc()函数后,文件指针移动到下一个字符。


完整的PHP文件系统参考

有关文件系统功能的完整引用,请转至我们的完整 PHP文件系统参考。


PHP练习

用练习来测试自己

锻炼:

打开文件,并写出正确的语法,以在文件结束时输出一个字符。

$myfile = fopen("webdict.txt", "r");
while(!($myfile)) {
  echo ($myfile);
}

 

 

 

  
  
下一篇: PHP文件创建/写

评论区

{{item['qq_nickname']}}
()
回复
回复
    相似文章