Home Backend Development PHP Problem How can I read a php file but not write data?

How can I read a php file but not write data?

Sep 15, 2021 pm 06:51 PM
php read file

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.

How can I read a php file but not write 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:

1

fopen(string $filename, string $mode[, bool $use_include_path = false[, resource $context]])

Copy after login

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:

modeDescription
rOpen 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.
wOpen 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.
aOpen 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.
xCreate 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() 函数实现此功能。该函数语法格式如下:

1

fgetc(resource $handle)

Copy after login

其中参数 $handle 为使用 fopen() 或 fsockopen() 成功打开的文件资源。

fgetc() 函数可以返回一个包含有一个字符的字符串,该字符是从 $handle 指向的文件中得到。当碰到 EOF 时返回 FALSE。

注意:fgetc() 函数可能返回布尔值 FALSE,也可能返回等同于 FALSE 的非布尔值。所以应该使用===运算符来测试此函数的返回值。

另外,fgetc() 函数可安全用于二进制对象,但不适用于读取中文字符串,因为一个中文通常占用 2~3 个字符。

【示例】使用 fgetc() 函数逐个字符的读取文件中的内容并输出。

1

2

3

4

5

6

7

8

9

10

11

<?php

    header("Content-Type: text/html;charset=utf-8");    //设置字符编码

    $handle = fopen(&#39;./test.txt&#39;, &#39;r&#39;);                 //打开文件

    if (!$handle) {                                     //判断文件是否打开成功

        echo &#39;文件打开失败!&#39;;

    }

    while (false !== ($char = fgetc($handle))) {        //循环读取文件内容

        echo $char;

    }

    fclose($handle);                                    //关闭文件

?>

Copy after login

fgets()和fgetss():逐行读取文件

fgets() 函数用于一次读取一行数据。函数的语法格式如下:

1

fgets(resource $handle[, int $length])

Copy after login

其中参数 $handle 是被打开的文件;参数 $length 为可选参数,用来设置读取的数据长度。函数能够实现从指定文件 $handle 中读取一行并返回长度最大值为 $length-1 个字节的字符串。在遇到换行符、EOF 或者读取了 $length-1 个字节后停止。如果忽略 $length 参数,则默认读取 1k(1024字节)长度。

【示例】使用 fgets() 函数逐行读取文件的内容并输出。

1

2

3

4

5

6

7

8

9

<?php

    $handle = @fopen("./test.txt", "r");

    if ($handle) {

        while (($info = fgets($handle, 1024)) !== false) {

            echo $info.&#39;<br>&#39;;

        }

        fclose($handle);

    }                               

?>

Copy after login

fgetss() 函数是 fgets() 函数的变体,用于读取一行数据,同时 fgetss() 函数会过滤掉读取内容中的 HTML 和 PHP 标记,函数的语法格式如下:

1

fgetss(resource $handle[, int $length[, string $allowable_tags]])

Copy after login

参数说明如下:

  • $handle:为被打开的文件;

  • $length:可选参数,用来设置要读取的数据长度;

  • $allowable_tags:可选参数,用来指定哪些标记不被去掉。

注意:fgetss() 函数在 PHP7.3 及之后的版本中已经弃用。

【示例】分别使用 fgets() 函数和 fgetss() 函数读取 index.html 文件并输出结果,看一看有什么区别。

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

<?php

    echo &#39;-------使用 fgets() 函数的输出结果:-------<br>&#39;;

    $handle = @fopen("index.html", "r");

    if ($handle) {

        while (!feof($handle)) {

            $buffer = @fgets($handle, 4096);

            echo htmlentities($buffer,ENT_QUOTES,"UTF-8").&#39;<br>&#39;;

        }

        fclose($handle);

    }

    echo &#39;-------使用 fgetss() 函数的输出结果:-------<br>&#39;;

    $handle = @fopen("index.html", "r");

    if ($handle) {

        while (!feof($handle)) {

            $buffer = @fgetss($handle, 4096);

            echo $buffer.&#39;<br>&#39;;

        }

        fclose($handle);

    }

?>

Copy after login

推荐学习:《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!

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 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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

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)

CakePHP Project Configuration CakePHP Project Configuration Sep 10, 2024 pm 05:25 PM

In this chapter, we will understand the Environment Variables, General Configuration, Database Configuration and Email Configuration in CakePHP.

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

CakePHP Date and Time CakePHP Date and Time Sep 10, 2024 pm 05:27 PM

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

CakePHP File upload CakePHP File upload Sep 10, 2024 pm 05:27 PM

To work on file upload we are going to use the form helper. Here, is an example for file upload.

CakePHP Routing CakePHP Routing Sep 10, 2024 pm 05:25 PM

In this chapter, we are going to learn the following topics related to routing ?

Discuss CakePHP Discuss CakePHP Sep 10, 2024 pm 05:28 PM

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

CakePHP Creating Validators CakePHP Creating Validators Sep 10, 2024 pm 05:26 PM

Validator can be created by adding the following two lines in the controller.

See all articles