Home >Backend Development >PHP Problem >How can I read a php file but not write data?
How to read but not write data in php files: 1. Use the "fopen('file path', 'r')" statement to open the file in a read-only manner; 2. Use fgetc( ), fgets(), fgetss() and other functions to read data.
The operating environment of this tutorial: windows7 system, PHP7.1 version, DELL G3 computer
In PHP, you can use the fopen() function Open the file in read-only mode for reading but not for writing data.
You can use the fopen() function in PHP to open a file or URL. If the opening is successful, the file pointer resource is returned; if the opening fails, FALSE is returned. The syntax format of this function is as follows:
fopen(string $filename, string $mode[, bool $use_include_path = false[, resource $context]])
The parameter description is as follows:
$ filename: is the URL of the file to be opened. This URL can be the absolute path in the server where the file is located, or it can be a relative path or a file in a network resource;
$mode: used Set how the file is opened (file mode). The specific value can be selected from the following table:
mode | Description |
---|---|
r | Open in read-only mode and point the file pointer to the file header. |
r | Open in read-write mode and point the file pointer to the file header. |
w | Open for writing, point the file pointer to the file header and truncate the file size to zero. Create the file if it does not exist. |
w | Open in read-write mode, point the file pointer to the file header and truncate the file size to zero. Create the file if it does not exist. |
a | Open for writing, pointing the file pointer to the end of the file. Create the file if it does not exist. |
a | Open in read-write mode and point the file pointer to the end of the file. Create the file if it does not exist. |
x | Create and open for writing, pointing the file pointer to the file header. If the file already exists, the fopen() call fails and returns FALSE and generates an E_WARNING level error message. Create the file if it does not exist. Applies to local files only. |
x | Create and open in read-write mode, other behaviors are the same as x. |
c | Only opens the file for writing, or creates the file if it does not exist. If the file exists, the file contents are not cleared and the file pointer is pointed to the file header. |
c | Open the file for reading and writing, and create the file if it does not exist. If the file exists, the file contents are not cleared and the file pointer is pointed to the file header. |
$use_include_path:可选参数,如果也需要在 include_path 中搜寻文件的话,可以将 $use_include_path 设为 1 或 TRUE;
$context:可选参数,在 PHP5.0.0 中增加了对上下文(Context)的支持。
读取文件数据,可以使用fgetc()、fgets()、fgetss()等函数
fgetc():从文件中读取一个字符
在对某一个字符进行查找、替换时,就需要有针对性地对某个字符进行读取,在 PHP 中可以使用 fgetc() 函数实现此功能。该函数语法格式如下:
fgetc(resource $handle)
其中参数 $handle 为使用 fopen() 或 fsockopen() 成功打开的文件资源。
fgetc() 函数可以返回一个包含有一个字符的字符串,该字符是从 $handle 指向的文件中得到。当碰到 EOF 时返回 FALSE。
注意:fgetc() 函数可能返回布尔值 FALSE,也可能返回等同于 FALSE 的非布尔值。所以应该使用===运算符来测试此函数的返回值。
另外,fgetc() 函数可安全用于二进制对象,但不适用于读取中文字符串,因为一个中文通常占用 2~3 个字符。
【示例】使用 fgetc() 函数逐个字符的读取文件中的内容并输出。
<?php header("Content-Type: text/html;charset=utf-8"); //设置字符编码 $handle = fopen('./test.txt', 'r'); //打开文件 if (!$handle) { //判断文件是否打开成功 echo '文件打开失败!'; } while (false !== ($char = fgetc($handle))) { //循环读取文件内容 echo $char; } fclose($handle); //关闭文件 ?>
fgets()和fgetss():逐行读取文件
fgets() 函数用于一次读取一行数据。函数的语法格式如下:
fgets(resource $handle[, int $length])
其中参数 $handle 是被打开的文件;参数 $length 为可选参数,用来设置读取的数据长度。函数能够实现从指定文件 $handle 中读取一行并返回长度最大值为 $length-1 个字节的字符串。在遇到换行符、EOF 或者读取了 $length-1 个字节后停止。如果忽略 $length 参数,则默认读取 1k(1024字节)长度。
【示例】使用 fgets() 函数逐行读取文件的内容并输出。
<?php $handle = @fopen("./test.txt", "r"); if ($handle) { while (($info = fgets($handle, 1024)) !== false) { echo $info.'<br>'; } fclose($handle); } ?>
fgetss() 函数是 fgets() 函数的变体,用于读取一行数据,同时 fgetss() 函数会过滤掉读取内容中的 HTML 和 PHP 标记,函数的语法格式如下:
fgetss(resource $handle[, int $length[, string $allowable_tags]])
参数说明如下:
$handle:为被打开的文件;
$length:可选参数,用来设置要读取的数据长度;
$allowable_tags:可选参数,用来指定哪些标记不被去掉。
注意:fgetss() 函数在 PHP7.3 及之后的版本中已经弃用。
【示例】分别使用 fgets() 函数和 fgetss() 函数读取 index.html 文件并输出结果,看一看有什么区别。
<?php echo '-------使用 fgets() 函数的输出结果:-------<br>'; $handle = @fopen("index.html", "r"); if ($handle) { while (!feof($handle)) { $buffer = @fgets($handle, 4096); echo htmlentities($buffer,ENT_QUOTES,"UTF-8").'<br>'; } fclose($handle); } echo '-------使用 fgetss() 函数的输出结果:-------<br>'; $handle = @fopen("index.html", "r"); if ($handle) { while (!feof($handle)) { $buffer = @fgetss($handle, 4096); echo $buffer.'<br>'; } fclose($handle); } ?>
推荐学习:《PHP视频教程》
The above is the detailed content of How can I read a php file but not write data?. For more information, please follow other related articles on the PHP Chinese website!