Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize the function of reading but not writing data in php file

2025-01-21 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article will explain in detail how php files can be read but can not write data function, Xiaobian think quite practical, so share to everyone as a reference, I hope you can read this article after harvest.

php file to read but not write data methods: 1, use the "fopen ('file path','r')" statement to open the file read-only;2, use fgetc(), fgets(), fgetss() and other functions to read data.

Operating environment of this tutorial: Windows 7 system, PHP 7.1 version, DELL G3 computer

In PHP, you can use the fopen() function to open a file read-only to read but not write data.

In PHP you can use the fopen() function to open files or URLs. Returns a pointer to the file resource if it opens successfully, or FALSE if it fails. The syntax of this function is as follows:

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

Parameters are described as follows:

$filename: The URL of the file to be opened. This URL can be an absolute path in the server where the file is located, or a relative path or a file in a network resource;

$mode: Used to set how files are opened (file mode). Specific values can be selected from the following table:

mode indicates that r is open in read-only mode, pointing the file pointer to the file header. r+ Open in read-write mode, pointing the file pointer to the file header. w Open on write, pointing the file pointer to the file header and truncating the file size to zero. Create the file if it does not exist. w+ Open in read-write mode, pointing the file pointer to the file header and truncating the file size to zero. Create the file if it does not exist. a Open in write mode, pointing the file pointer to the end of the file. Create the file if it does not exist. a+ Open read-write, pointing the file pointer to the end of the file. Create the file if it does not exist. x is created and opened in write mode, pointing the file pointer to the file header. If the file already exists, the fopen() call fails and returns FALSE, generating an E_WARNING error message. Create the file if it does not exist. Available only for local files. x+ is created and opened read-write, otherwise behaves like x. c Only open the file for writing, or create it if it doesn't exist. If the file exists, the file contents are not emptied and the file pointer is pointed to the file header. c+ Open the file for reading and writing, or create it if it doesn't exist. If the file exists, the file contents are not emptied and the file pointer is pointed to the file header.

$use_include_path: Optional parameter, if you also need to search for files in include_path, you can set $use_include_path to 1 or TRUE;

$context: Optional parameter. Context support was added in PHP 5.0.0.

To read file data, you can use fgetc(), fgets(), fgetss(), etc.

fgetc(): reads a character from a file

When searching and replacing a character, you need to read a character specifically. In PHP, you can use the fgetc() function to achieve this function. The syntax for this function is as follows:

fgetc(resource $handle)

where parameter $handle is the file resource successfully opened using fopen() or fsockopen().

The fgetc() function returns a string containing a character from the file $handle points to. Returns FALSE when EOF is encountered.

Note: The fgetc() function may return FALSE, a Boolean value, or a non-Boolean value equivalent to FALSE. So you should use the === operator to test the return value of this function.

In addition, the fgetc() function is safe for binary objects, but it is not suitable for reading Chinese strings because a Chinese character usually takes 2 - 3 characters.

[Example] Use fgetc() function to read the contents of a file character by character and output it.

fgets() and fgetss(): Read the file line by line

The fgets() function reads data one line at a time. The syntax of the function is as follows:

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

where parameter $handle is the file being opened; parameter $length is an optional parameter used to set the length of the data being read. The $handle () function reads a line from the specified file $handle and returns a string with a maximum length of $length-1 bytes. Stops after a newline, EOF, or $length-1 byte read. If the $length parameter is omitted, the default read length is 1k (1024 bytes).

Use the fgets() function to read the contents of a file line by line and output it.

The fgetss() function is a variant of the fgets() function, which reads a line of data and filters out HTML and PHP tags in the read content. The syntax of the function is as follows:

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

Parameters are described as follows:

$handle: is the open file;

$length: Optional parameter, used to set the length of data to be read;

$allowable_tags: Optional parameter to specify which tags are not removed.

Note: The fgetss() function has been deprecated in PHP 7.3 and later.

[Example] Use fgets() function and fgetss() function to read index.html file and output the results respectively, see what the difference is.

About "php file how to achieve can read but can not write data function" This article is shared here, I hope the above content can be of some help to everyone, so that you can learn more knowledge, if you think the article is good, please share it to let more people see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report