PHP Development File System

Source: Internet
Author: User
Tags fread readfile

1. Open/Close File
Open/Close files use the fopen () function and the fclose () function, respectively. When you open a file, you should pay special attention to accidentally deleting the contents of the file.
(1) Open file
The fopen () function, the syntax format is as follows:
Resource fopen (string filename,string mode [, BOOL Use_include_path]);
The parameter filename is the file name of the containing path to open, either a relative path or an absolute path. The parameter mode is the open mode, as shown in the following table:

(2) Close the file
The file should be closed after the operation of the file, otherwise it may cause an error. Use the fclose () function in PHP to close the file with the following syntax:
BOOL Fclose (resource handle);
The function closes the file that points to handle and returns False if successful returns true. Where the file pointer must be valid and is a successfully opened file through the fopen () function.
The instance code is as follows:

<?php$_open =fopen("../fiel.txt","rb");   //打开文件...                             //对文件的操作fclose($_open);                //关闭文件?>

2, read and write files
Reading data from a file, you can read a character, a line of string, or an entire file. It can also be a string of any length.
(1) Read the entire file: ReadFile (), file (), and file_get_contents () functions.
A, ReadFile () function
The ReadFile () function is used to read a file and write it to the output buffer, which returns False if an error occurs. The function syntax is as follows:
int ReadFile (string filename);
Attention:
Using the ReadFile (0 function does not need to open/close the file, just enter the path to the file.) You do not need to echo/print the output statement.

b, the file () function
The file () function can also read the data for the entire document, but the file () function stores the contents of the files in the array as rows. Include line breaks. Returns False if it fails. The syntax format is as follows:
Array file (string filename);

C, file_get_contents () function
This function reads the contents of the file into a string. If the offset and MaxLen parameters are available, the contents of length maxlen are read at the specified position at offset at the parameter. Returns False if the read fails. The syntax format is as follows:
Sting file_get_contents (String Filename[,int offset[,int MaxLen]]);
This function applies to binary objects. is the preferred method of reading the contents of the entire file into a string.

The sample code is as follows:

<table Width =" border " = "1" cellspacing ="0"  cellpadding ="0"><tr> <TD Width ="253" height =" align " = "Right" valign ="Middle" scope ="col">Use the ReadFile () function to read the contents of the file:</td> <TD Width = "241" height =" align " = "Center" valign ="Middle" scope ="col">   <?php ReadFile ("Tm.txt"); Echo  "\ n"; ?>  </td></tr><tr> <TD Height =" align " = "Right" valign ="Middle ">Use the file () function to read the contents of the files:</td> <TD Height =" align " = "Center" valign = "Middle">  <?php $f _arr =file ("Tm.txt"); foreach ($f _arr  as $count) { echo $count. "\ n"; } ?>  </td></tr><tr>   <TD Width =" max" height = " align " = "Right" valign ="Middle" scope ="col">Use the file_get_contents () function to read the contents of the file:</td>   <TD Height =" align " = "Center" valign ="Middle"  Scope ="col">    <?php$f _chr =file_get_contents (' tm.txt '); Echo $f _chr.   "\ n"; ?>       </td></tr></table>

The results of the operation are as follows:

(2) read one row of data: Fgets () and FGETSS ()
The fgets () function is used to read a row of data at once, with the following syntax:
String fgets (int handle[,int length]);
The parameter handle is the file that is opened, and the length of the parameter is the amount of data to be read. The function can implement a string that reads a maximum length of length-1 bytes from the handle. Stops after encountering a newline character, EOF, or after reading a length-1 word. If the length parameter is omitted, then read until the line ends.

The FGETSS () function is a variant of the fgets () function that reads a row of data. At the same time, the fgets () function filters out HTML and PHP tags in the read content.
The syntax format is as follows:
String Fgetss (Resource Handle[,int length[,string allowable_tags])
This function can filter out any HTML and PHP tags from the file. You can use the Allowable_tags parameter to control which tags are not filtered out.
The following is an example of using the Fgets () function and the FGETSS () function, which is the following code:

<table Border ="1"><tr><TD Align ="Right" valign = "Middle" scope ="Col" >Use the Fgets () function:</td><TD Align ="center" valign ="Middle" scope ="col"> <?php $open =fopen (' jacktest.php ', ' RB ');  while (!feof ($open)) { echo fgets ($open);} Fclose ($open); ?> </td></tr><tr><TD Align ="Right" valign = "Middle" scope ="Col" >Use the FGETSS () function:</td><TD Align ="center" valign ="Middle" scope ="col"> <?php $handle =fopen (' jacktest.php ', ' RB ');  while (!feof ($handle)) { echo fgetss ($handle);} Fclose ($handle); ?> </td></tr></table>

The results of the operation are as follows:

(3) fgetc () read one character
When a character is searched, the substitution requires a specific reading of a character. The syntax format is as follows:
String fgetc (Resource handle)
The sample code is as follows:

<?php $handle =fopen(‘123.txt‘, ‘rb‘);while (false !=($chr =fgetc($handle))){     echo $chr."<br>";}fclose($handle);?>

The contents of the 123.txt file are as follows:

The results of the operation are as follows:

(4) Fread () reads a string of any length
The syntax format is as follows:
String fread (resource[,int length])
The sample code is as follows:

<?php $handle =fopen(‘123.txt‘, ‘rb‘);echo fread($handle, 40)."<br>";      //读取四十个字节的字符串。fclose($handle);?>

The contents of 123.txt are as follows:

The results of the operation are as follows:

(5) Writing data to a file
In PHP, you want to write data in a file by using the Fwrite () and file_put_contents () functions.
The fwrite () syntax is as follows:
int fwrite (Resource handle,string string[,int length])
The function writes a string to the handle, and if length is established, it is written to stop at the end of the long word. If not, write all the contents of the string.
The file_put_contents () function php5 the new function. The syntax is as follows:
int file_put_contents (string filename,string data[,int flags])
FileName bit the path name of the file to write the data to.
Data is to be written
Flags can be file_include_path,file_append or LOCK_X,LOCK_EX for exclusive locking.
Let's compare the following separately. Use the difference between the function fopen (), fwrite (), fclose (), and file_put_contents ().
The sample code is as follows:

<?php $filepath="1.txt";$str="It's a good day and I'm in a good mood.";Echo "Write file:<br> with the fwrite () function";$fopen=fopen ($filepath,"WB")or  die("file does not exist"); Fwrite ($fopen,$str); Fclose ($fopen); ReadFile ($filepath);Echo "Write to file:<br> with the file_put_contents () function";$str 1="This is using the file_put_contents () function to write to the file 1111"; File_put_contents ($filepath,$str 1); ReadFile ($filepath);?>

The results of the operation are as follows:

2, the Operation of the file
The commonly used file manipulation functions are as follows:

PHP Development File System

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.