PHP writing or appending data to a file _php tutorial

Source: Internet
Author: User
Tags mysql import

PHP writes or appends data to a file


PHP to write or append data to a file there are two ways, one is fopen, and the other is file_put_contents, this article briefly introduces the two methods of the specific usage, the need for friends to see.

(1) fopen

The fopen () function opens the file or URL and returns FALSE if the open fails.

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

Parameter description

FileName is required. Specifies the file or URL to open.
Mode required. Specifies the type of access required to the file/stream. The possible values are shown in the following table.
Include_path is optional. If you also need to retrieve the file in Include_path, you can set the parameter to 1 or TRUE.
The context is optional. Specifies the environment for file handles. Context is a set of options that can modify the behavior of a stream.

Possible values for the mode parameter

Mode Description
"R" Read-only opens, pointing the file pointer to the file header.
"R+" Read-write mode opens, pointing the file pointer to the file header.
"W" The Write method opens, pointing the file pointer to the file header and truncating the file size to zero. If the file does not exist, try to create it.
"W+" Read-write mode opens, pointing the file pointer to the file header and truncating the file size to zero. If the file does not exist, try to create it.
A Write to open, pointing the file pointer to the end of the file. If the file does not exist, try to create it.
"A +" Read-write mode opens, pointing the file pointer to the end of the file. If the file does not exist, try to create it.
"X"

Creates and opens in 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. If the file does not exist, try to create it.

This specifies o_excl| for the underlying open (2) system call The o_creat tag is equivalent.

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

"X+"

Creates and opens read-write, 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. If the file does not exist, try to create it.

This specifies o_excl| for the underlying open (2) system call The o_creat tag is equivalent.

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

Write content in append form

 

(2) File_put_contents

The file_put_contents () function is used to write a string to a file, successfully returning the number of bytes written to the data in the file, and FALSE if the failure is unsuccessful.

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

Parameter description

FileName to write the data to
Data to be written to. The type can be String,array (but not a multidimensional array), or a stream resource
Flags are optional, which 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 appended to the end of the file
LOCK_EX: Lock the file
Context is optional, and context is a set of options through which you can modify text properties

For example:

  

Write content in append form

When the flags parameter value is set to File_append, the new data is written in such a way that the content is appended to the contents of the existing file, for example:

   

Tips

The behavior of file_put_contents () is actually equivalent to calling fopen (), fwrite (), and the fclose () function in turn.

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

If the file exists, the contents of the file are emptied by default, and you can set the flags parameter value to File_append to avoid.

The File_put_contents function can be used safely with binary objects.

Articles you may be interested in

    • PHP determines the safest and most realistic solution for uploading file types
    • PHP gets a method for each month in a certain period of time, returning an array of these months
    • PHP gets all the files in the directory and saves the results to an array of programs
    • Compress website js and CSS files using PHP's gzip compression to speed up website access
    • PHP parsing file header information to determine the type of upload file
    • Right-click Add New window to XP folder open folder
    • MySQL Import and export by command. An instance of a SQL file backup data operation
    • PHP failed to upload large files solution

http://www.bkjia.com/PHPjc/984181.html www.bkjia.com true http://www.bkjia.com/PHPjc/984181.html techarticle PHP writes or appends data to a file PHP writes or appends data to a file in two ways, one is fopen, and the other is file_put_contents, this article briefly introduces the two methods ...

  • Related Article

    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.