Home > Backend Development > PHP Tutorial > PHP programming: basic operations of file reading and writing

PHP programming: basic operations of file reading and writing

WBOY
Release: 2023-09-06 14:10:02
Original
1186 people have browsed it

PHP programming: basic operations of file reading and writing

PHP Programming: Basic Operations of File Reading and Writing

Title: PHP Programming: Basic Operations of File Reading and Writing

Article:

PHP is a widely used server-side scripting language that provides many file operation functions. In PHP, we can implement file reading and writing operations through simple code. This article will introduce the basic operations of file reading and writing in PHP to help readers better understand and apply these functions.

  1. File reading operation

In PHP, you can use the following function to read the contents of the file:

  • fopen(): Opens a file and returns a file pointer.
  • fread(): Read data of the specified length from the open file.
  • fclose(): Close the open file.

The following is a simple sample code that demonstrates how to read the contents of a file using PHP:

$file = fopen("example.txt", "r");

if ($file) {
    while (($line = fgets($file)) !== false) {
        echo $line;
    }

    fclose($file);
} else {
    echo "无法打开文件!";
}
Copy after login

In the above code, we use the fopen() function to open a file, And specified to open in read-only mode (r). Then, use a while loop and the fgets() function to read the contents of the file line by line and output it to the browser. Finally, use the fclose() function to close the file.

  1. File writing operation

In PHP, you can use the following function to write the contents of a file:

  • fopen(): Opens a file and returns a file pointer.
  • fwrite(): Write data to the open file.
  • fclose(): Close the open file.

The following is a simple sample code that demonstrates how to write content to a file using PHP:

$file = fopen("example.txt", "w");

if ($file) {
    $text = "Hello, World!";

    fwrite($file, $text);

    fclose($file);
} else {
    echo "无法打开文件!";
}
Copy after login

In the above code, we use the fopen() function to open a file, And specified to open in write mode (w). Then, use the fwrite() function to write the specified content to the file. Finally, use the fclose() function to close the file.

It should be noted that if the file does not exist, using "w" mode will create a new file; if the file already exists, the file content will be cleared and new content will be written. If you just want to append content to the end of the file, you can open the file in "a" mode.

Summary:

This article introduces the basic operations of file reading and writing in PHP. By using functions such as fopen(), fread(), fwrite() and fclose(), you can Conveniently implement file reading and writing operations. Readers can flexibly use these functions to handle file-related tasks according to their own needs. At the same time, in practical applications, attention should also be paid to issues such as exception handling and file permissions to ensure the smooth progress of file operations.

The above is the detailed content of PHP programming: basic operations of file reading and writing. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template