Handling and operation of PHP files

Source: Internet
Author: User
Tags fread php example readfile

For a long time did not read the PHP manual, some of the knowledge of the operation of the file has not been learned, now tutorial, by the way:

1. opening of the file:fopen () The first parameter of this function contains the name of the file to be opened, and the second parameter specifies which mode to use to open the file

R Open the file as read-only. The file pointer starts at the beginning of the file.
W Open the file as write-only. Delete the contents of the file or create a new file if it does not exist. The file pointer starts at the beginning of the file.
A Open the file as write-only. The existing data in the file is retained. The file pointer starts at the end of the file. Creates a new file if the file does not exist.
X Create a new file for write-only. Returns FALSE and an error if the file already exists.
r+ Open the file as read/write, and the file pointer starts at the beginning of the file.
w+ Open the file for read/write. Delete the contents of the file or create a new file if it does not exist. The file pointer starts at the beginning of the file.
A + Open the file for read/write. The data already in the file will be retained. The file pointer starts at the end of the file. Creates a new file if it does not exist.
x+ Create a new file for read/write. Returns FALSE and an error if the file already exists.

Text file "Webdictionary.txt":

AJAX ==== = = = ExtensibleMarkup Language

PHP Example:

<! DOCTYPE html>PHP $myfile fopen ("webdictionary.txt", "Rdie" ("Unable to open file!" ); Echo fread ($myfile,filesize("Webdictionary.txt")); fclose ($myfile);? ></body>

Operation Result:

AJAX = asynchronous JavaScript and XML CSS = cascading Style Sheets HTML = Hyper Text Markup Language php = PHP Hypertext Preprocessor SQL = structured Query Language SVG = Scalable Vector Graphics XML = extensible Markup Language

2. file closed:fclose () The name of the file to be closed (or a variable that holds the file name) is commented: It is a good programming habit to close all of the files after they are exhausted.

Example:

<? PHP $myfile fopen ("Webdictionary.txt", "R"); // some code to be executed .... fclose ($myfile);? >

3. file creation :fopen () when the open file does not exist and is opened to write (W) or add (a) when the file is created.

$myfile fopen ("Testfile.txt", "W")

4. file reads :ReadFile ( ) and fread () Difference: the ReadFile () function reads the file and writes it to the output buffer without the need for code to manipulate the open process ; Fread () Function reads an open file, the first parameter contains the file name of the file to be read, and the second parameter specifies the maximum number of bytes to be read

Example:

<? PHP Echo ReadFile ("Webdictionary.txt");? >

fread ($myfile,filesize("Webdictionary.txt"));

Read single-line file:fgets () to read a single line from a file

<? PHP $myfile fopen  die ("Unable to open file!" ); Echo fgets ($myfile); fclose ($myfile);? >

Readsingle character: Fgetc () to read individual characters from a file

<? PHP $myfile fopen  die ("Unable to open file!" ); // output single character until End-of-file  while (! feof ($myfile)) {  echofgetc($myfile);} fclose ($myfile);? >

5. check if the bottom of the file has been reached:feof ()

<? PHP $myfile fopen  die ("Unable to open file!" ); // output single line until End-of-file  while (! feof ($myfile)) {  echofgets($myfile). "<br>";} fclose ($myfile);? >

6. write to File:fwrite () The first parameter contains the file name of the file to be written, and the second argument is the string to be written.

The following example writes the name to a new file named "Newfile.txt":

<? PHP $myfile fopen  die ("Unable to open file!" ); $txt = "Bill gates\n"; fwrite ($myfile$txt); $txt = "Steve jobs\n"; fwrite ($myfile$txt); fclose ($myfile);? >

If we open the "newfile.txt" file, it should look like this:

Bill Gatessteve Jobs

7. coverage of the article:overwritimg () What happens when you write to an existing file. All existing data is erased and starts with a new file.

 <? php   $myfile  = fopen  ("Newfile.txt", "w") or " Span style= "COLOR: #0000ff" >die  ("Unable to open file!"   $txt  = "Mickey mouse\n" ;    overwritimg   ( $myfile ,  $txt    $txt  = "Minnie mouse\n" ;    overwritimg   ( $myfile ,  $txt   fclose  ( $myfile  ? 

If we open this "newfile.txt" file Now, Bill and Steve have disappeared, leaving only the data we have just written:

Mickey Mouseminnie Mouse

Handling and operation of PHP files

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.