PHP writes or appends data to a file

Source: Internet
Author: User

PHP writes or appends data to a file

Php can write or append data to a file in two ways: fopen and file_put_contents. This article briefly introduces the usage of the two methods, if you need it, you can check it out.

(1) fopen

The fopen () function opens a file or URL. If the file fails to be opened, this function returns FALSE.

Syntax: fopen (filename, mode, include_path, context)

Parameter description

Filename is required. Specifies the file or URL to open.
Mode is required. Specifies the access type of the file/stream. Possible values are shown in the table below.
Optional. If you also need to retrieve the file in include_path, you can set this parameter to 1 or TRUE.
Context is optional. Specifies the file handle environment. Context is a set of options that can modify the behavior of a stream.

Possible value of the mode Parameter

Mode Description
"R" Open the file 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 in writing mode, point the file pointer to the file header, and cut the file size to zero. If the file does not exist, try to create it.
"W +" Open in read/write mode, point the file pointer to the file header, and cut the file size to zero. If the file does not exist, try to create it.
"" Open in writing mode and point the file pointer to the end of the file. If the file does not exist, try to create it.
"A +" Open in read/write mode and point the file pointer to the end of the file. If the file does not exist, try to create it.
"X"

Create and open the file in writing mode, and point the file pointer to the file header. If the file already exists, fopen () fails to be called, returns FALSE, and generates an E_WARNING-level error message. If the file does not exist, try to create it.

This is equivalent to specifying the O_EXCL | O_CREAT mark for the underlying open (2) System Call.

This option is supported by PHP 4.3.2 and later versions and can only be used for local files.

"X +"

Create and open the file in read/write mode, and point the file pointer to the file header. If the file already exists, fopen () fails to be called, returns FALSE, and generates an E_WARNING-level error message. If the file does not exist, try to create it.

This is equivalent to specifying the O_EXCL | O_CREAT mark for the underlying open (2) System Call.

This option is supported by PHP 4.3.2 and later versions and can only be used for local files.

Write content as an append

<?php$fp=fopen('test.txt','a');

(2) file_put_contents

The file_put_contents () function is used to write strings to a file. The number of bytes written to the file is returned successfully. If the string fails, FALSE is returned.

Syntax: int file_put_contents (string filename, string data [, int flags [, resource context])

Parameter description

File Name of the data to be written to filename
The data to be written. The type can be string, array (but not multi-dimensional array), or stream resource.
(Optional) flags. It specifies how to open/write files. Possible values:
FILE_USE_INCLUDE_PATH: Check the built-in path of the filename copy.
FILE_APPEND: writes data to the end of a file in append mode.
LOCK_EX: Lock the file
Optional. context is a set of options. You can use it to modify text attributes.

For example:

<? Phpecho file_put_contents ("test.txt", "www.phpernote.com"); // output: 17

Write content as an append

When the flags parameter value is set to FILE_APPEND, it indicates writing new data by appending content to the content of an existing file. For example:

<?phpfile_put_contents("test.txt","www.phpernote.com",FILE_APPEND);

Prompt

File_put_contents () actually calls the fopen (), fwrite (), and fclose () functions in turn.

If the file does not exist, the file is created, which is equivalent to the fopen () function.

If the file exists, content in the file will be cleared by default. You can set the flags parameter value to FILE_APPEND to avoid this.

The file_put_contents function can be safely used for binary objects.

Articles you may be interested in
  • PHP is the safest and most practical solution to determine the type of uploaded files
  • Php gets the method of each month in a certain period of time, and returns an array composed of these months
  • Php obtains all the files in the directory and saves the results to the array program.
  • Use the GZip compression function of PHP to compress website JS and CSS files to accelerate Website access.
  • PHP analyzes the file header information to determine the type of the uploaded file
  • Right-click the xp folder and add a new window to open the folder
  • MySQL uses commands to import and export instances for. SQL file backup data operations
  • Solution to php File Upload failure

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.