Home Backend Development PHP Tutorial Basic knowledge of PHP file processing (summary)

Basic knowledge of PHP file processing (summary)

Jan 31, 2019 pm 03:29 PM
php file processing



Any application requires file handling. For certain tasks to be accomplished, files need to be processed. File handling in PHP Similar to file handling which is done by using any programming language like C, PHP has many functions to handle normal files.

Basic knowledge of PHP file processing (summary)

These functions are:

1.fopen() - The PHP fopen() function is used to open a file. The first parameter of fopen() contains the name of the file to be opened, and the second parameter specifies the mode in which the file needs to be opened. For example,

<?php 
$file = fopen(“demo.txt”,&#39;w&#39;); 
?>
Copy after login

The file can be opened using any of the following modes:

"w" - Open the file for writing. If the file does not exist, create a new file or delete the file contents if the file already exists.

"r" - The file is opened for read-only use.

"a" - The file is opened for writing. The file pointer points to the end of the file. Preserve existing data in the file.

"w" - Open the file for reading and writing. If the file does not exist, create a new file or delete the file contents if the file already exists.

"r " - Open file for reading/writing.

"a " - Open file for writing/reading. The file pointer points to the end of the file. Preserve existing data in the file. Create a new file if the file does not exist.

"x" - Create new file for write only.

2.fread() - - After using fopen() to open the file, use fread() to read the data content. It requires two parameters. One is the file pointer and the other is the file size in bytes, for example,

<?php 
$filename = "demo.txt"; 
$file = fopen( $filename, &#39;r&#39; ); 
$size = filesize( $filename ); 
$filedata = fread( $file, $size ); 
?>
Copy after login

3.fwrite() - You can use the fwrite() function to create a new file or copy text Append to existing file. The parameters of the fwrite() function are the file pointer and the text to be written to the file. It can contain an optional third parameter, which specifies the length of text to be written, for example,

<?php 
$file = fopen("demo.txt", &#39;w&#39;); 
$text = "Hello world\n"; 
fwrite($file, $text); 
?>
Copy after login

4.fclose() - Use the fclose() function to close the file. Its parameters are files that need to be closed, for example,

<?php 
$file = fopen("demo.txt", &#39;r&#39;); 
fclose($file); 
?>
Copy after login

This article is an introduction to the basic knowledge of PHP file processing. I hope it will be helpful to friends in need!



The above is the detailed content of Basic knowledge of PHP file processing (summary). 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

Video Face Swap

Video Face Swap

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

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)

How to create CSV file in PHP How to create CSV file in PHP Jun 11, 2023 pm 02:51 PM

The CSV (Comma-SeparatedValues) file format is widely used for data exchange and import/export jobs. In PHP, CSV files can be easily created using the built-in file manipulation functions and CSV functions. In this article, we will learn how to create CSV files using PHP. Step 1: Create a CSV file. To create a CSV file, you first need to open a file handle and set the file's opening mode. In this example, we open the file for writing, and if the file does not exist,

Getting Started with PHP File Processing: Deep Understanding of the Basic Steps of Reading and Writing Getting Started with PHP File Processing: Deep Understanding of the Basic Steps of Reading and Writing Sep 06, 2023 am 08:43 AM

Getting Started with PHP File Handling: In-depth Understanding of the Basic Steps of Reading and Writing File handling is a very common and important task in PHP development. Whether it is reading the contents of a file or writing data to a file, it can be achieved through the built-in functions provided by PHP. This article will introduce the basic steps of PHP file processing and provide some code examples for reference. 1. Basic steps for reading files Reading files is an operation we often need to perform when processing files. Here are the basic steps for reading a file: use fopen(

How to perform file operations in PHP7.0? How to perform file operations in PHP7.0? May 26, 2023 pm 03:51 PM

In the Internet era, file operations have become one of the most common operations for programmers. As a popular server-side scripting language, PHP also has powerful file operation functions. This article will introduce how to perform file operations in PHP7.0, including operations such as opening, reading, writing, closing, and deleting files. At the same time, we will also introduce some common file processing functions to help readers better use PHP for file operations. Opening files In PHP, our commonly used file opening function is fopen(). This function requires two

PHP file reading, writing and directory operations: How to deal with them? PHP file reading, writing and directory operations: How to deal with them? Jun 30, 2023 am 10:16 AM

How to handle file reading, writing and directory operations in PHP? As a widely used server-side scripting language, PHP plays an important role in web development. In many projects, we need to read, write and directory operations on files in order to store and manage data. This article will introduce common methods and techniques on how to handle file reading, writing and directory operations in PHP. 1. File read and write operations. Opening and closing files. To read and write files, you first need to use the fopen function to open the file. This function needs to receive two parameters.

PHP file processing experience summary: tips and experiences to achieve efficient reading and writing PHP file processing experience summary: tips and experiences to achieve efficient reading and writing Sep 06, 2023 pm 12:24 PM

Summary of PHP file processing experience: Tips and experiences for achieving efficient reading and writing In web development, file processing is a common task. Whether reading configuration files or writing files uploaded by users, handling files is an essential skill for PHP programmers. This article will share some tips and experiences for achieving efficient reading and writing, and use code examples to deepen understanding. Reading files Reading files is one of the common operations. Here are several common methods for reading files: 1.1file_get_con

Getting Started with PHP File Processing: Detailed Basic Steps for Reading and Writing Getting Started with PHP File Processing: Detailed Basic Steps for Reading and Writing Sep 06, 2023 am 11:16 AM

Getting Started with PHP File Handling: Detailed Basic Steps for Reading and Writing Overview: Processing files is a very common task in web development. As a powerful server-side scripting language, PHP provides a wealth of file processing functions and methods, which can easily read and write file contents. This article will introduce the basic steps of reading and writing files using PHP and provide corresponding code examples. Reading file contents: Reading file contents is one of the common file processing tasks. PHP provides multiple functions and methods to achieve this

A complete list of essential file processing functions for PHP development A complete list of essential file processing functions for PHP development Jun 20, 2023 am 09:00 AM

PHP is a high-level programming language widely used in website development and back-end services. As developers, we need to handle a large number of files, such as reading and writing files, creating directories, deleting files, etc. In this article, we will introduce you to some common operations and techniques of PHP file processing functions to help developers better process files. 1. File processing function fopen(): opens a file and returns a file pointer, which can be used to read or write files. fclose(): Close an open file pointer. fread

How to use file and directory handling functions in PHP? How to use file and directory handling functions in PHP? Jul 25, 2023 am 09:58 AM

How to use file and directory handling functions in PHP? In web development, working with files and directories are common tasks. PHP provides a series of file and directory processing functions, allowing developers to easily operate files and directories. First, let's look at how to create, delete, and write files. Creating a File Use PHP's file_put_contents function to write contents to a file and create a new file. The following is a sample code: $file='test.txt';

See all articles