PHP creates a file, writes data to the file, overwrites, and appends the implementation code,

Source: Internet
Author: User
Tags php write

PHP creates a file, writes data to the file, overwrites, and appends the implementation code,

Functions are used to create files.

Fopen (string filename, string mode)
The filename parameter is the name of the created file.
How does the mode parameter open the file filename?
The list of possible mode values is as follows:

Mode description

'R' is opened in read-only mode. It directs the file pointer to the file header.
Open in 'r + 'read/write mode and point the file pointer to the file header.
Open in 'W' 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.
Open in 'W + '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 the 'A' write mode and point the file pointer to the end of the file. If the file does not exist, try to create it.
Open in 'a + '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' is created and opened in writing mode. It directs 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 + 'is created and opened in read/write mode. The file Pointer Points 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.
The following code creates a file named Demo in txt format by using the fopen function, and writes "Welcome To ItCodeWorld! "Data.

<? Php // The two files to be created $ TxtFileName = "Demo.txt"; // write the specified file in read/write mode, if the file is not saved, create if ($ TxtRes = fopen ($ TxtFileName, "w +") ===false) {echo ("create writable file :". $ TxtFileName. "failed"); exit ();} echo ("creating Writable Files ". $ TxtFileName. "successful! </Br> "); $ StrConents =" Welcome To ItCodeWorld! "; // If (! Fwrite ($ TxtRes, $ StrConents) {// write information to the file echo ("attempting to file". $ TxtFileName. "writing". $ StrConents. "failed! "); Fclose ($ TxtRes); exit ();} echo (" attempt to write ". $ TxtFileName." To File ". $ StrConents." succeeded! "); Fclose ($ TxtRes); // close the pointer?>

Create a file in PHP-fopen ()
The fopen () function is also used to create files. It may be a bit confusing, but in PHP, the functions used to create a file are the same as those used to open the file.
If you use fopen () to open a file that does not exist, this function will create a file, assuming that the file is opened to write (w) or add ().
The following example creates a new file named "testfile.txt. This file will be created in the same directory where PHP code is located:
Instance
$ Myfile = fopen ("testfile.txt", "w ")
PHP File Permissions
If an error occurs when you try to run this code, check whether you have the permission to access the PHP file that writes information to the hard disk.
PHP Write File-fwrite ()
The fwrite () function is used to write files.
The first parameter of fwrite () contains the name of the file to be written, and the second parameter is the Written string.
The following example writes the name to a new file named "newfile.txt:
Instance

<?php$myfile = fopen("newfile.txt", "w") or die("Unable to open file!");$txt = "Bill Gates\n";fwrite($myfile, $txt);$txt = "Steve Jobs\n";fwrite($myfile, $txt);fclose($myfile);?>

Please note that we wrote it twice to the file "newfile.txt. Every time we write data to a file, the string $ txt we send contains "Bill Gates" for the first time and "Steve Jobs" for the second time ". After writing, we use the fclose () function to close the file.
If we open the "newfile.txt" file, it should be like this:
Bill Gates
Steve Jobs

PHP Overwriting)

If "newfile.txt" contains some data, we can show what happened when writing an existing file. All existing data is erased and starts with a new file.
In the following example, we open an existing file "newfile.txt" and write some new data to it:
Instance

<?php$myfile = fopen("newfile.txt", "w") or die("Unable to open file!");$txt = "Mickey Mouse\n";fwrite($myfile, $txt);$txt = "Minnie Mouse\n";fwrite($myfile, $txt);fclose($myfile);?>

If we open the mongonewfile.txt file, both Bill and Steve will disappear, leaving only the data we just written:

Mickey Mouse
Minnie Mouse


Line breaks '\ n' and carriage returns' \ R'
As the name suggests, a line break is another line, and a carriage return is the beginning of a line. Therefore, the carriage return character of the file is usually called a carriage return line break.

'\ N' 10 newline)
'\ R' 13 press enter (return)

It can also be '\ x0a' and' \ x0d'. (hexadecimal)
In windows, the carriage return line character is "\ r \ n", but it does not exist in Linux or other systems.
When parsing the content of a text or other format file, you often need to determine the line break of the carriage return. In this case, you must determine both "\ r \ n" and "\ n ".
When writing a program, you may get a line and trim it out of '\ R' to get the string you need.

Articles you may be interested in:
  • PHP File Creation (folder) and directory operation code
  • PHP retrieves the directory under the specified directory and creates a file under the obtained directory. multiple platforms
  • Php generates sample code for automatically creating folders and uploading files
  • The mkdir () function of php is used to create a secure folder for permission setting.

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.