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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

PHP and Python each have their own advantages, and the choice should be based on project requirements. 1.PHP is suitable for web development, with simple syntax and high execution efficiency. 2. Python is suitable for data science and machine learning, with concise syntax and rich libraries.

PHP is still dynamic and still occupies an important position in the field of modern programming. 1) PHP's simplicity and powerful community support make it widely used in web development; 2) Its flexibility and stability make it outstanding in handling web forms, database operations and file processing; 3) PHP is constantly evolving and optimizing, suitable for beginners and experienced developers.

PHP and Python have their own advantages and disadvantages, and the choice depends on project needs and personal preferences. 1.PHP is suitable for rapid development and maintenance of large-scale web applications. 2. Python dominates the field of data science and machine learning.

PHP is suitable for web development, especially in rapid development and processing dynamic content, but is not good at data science and enterprise-level applications. Compared with Python, PHP has more advantages in web development, but is not as good as Python in the field of data science; compared with Java, PHP performs worse in enterprise-level applications, but is more flexible in web development; compared with JavaScript, PHP is more concise in back-end development, but is not as good as JavaScript in front-end development.

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.
