Tips for using various functions in PHP file operations _ PHP Tutorial

Source: Internet
Author: User
Function application skills in php file operations. 1. open the file: fopen (file name, mode); mode: r: Read-only r +: read/write w: Write-only. Open and clear the file content. If the file does not exist, create the file. W +: read/write. Open and clear

1. open the file: fopen ("file name", "mode ");

Mode:

R: Read-only
R +: Read/Write
W: write only. Open and clear the file content. If the file does not exist, create the file.
W +: read/write. Open and clear the file content. If the file is not saved, the file is created.
A: append. Open and write to the end of the file. If the file does not exist, a new file is created.
A +: Read/append. Write content to the end of the file to keep the file content.

2. close the file: fclose ();

For example:

 
 
  1. < ?
  2. $file=fopen("test.txt","r");
  3. //some code be executed
  4. fclose=($file);
  5. ?>

3. check whether the file end is reached: feof ();

Example: if (feof ($ file) {echo "end of file ";}

4. read files row by row: fgets ();

For example:

 
 
  1. $file=fopen("test.txt","r");
  2. while(!feof($file)){
  3. echo fgets($file)."< br/>";
  4. }
  5. fclose($file);

5. read files by character: fgetc ()

6. check whether the file exists: file_exists (); if yes, true is returned; otherwise, false is returned.

7. read the file to an array: $ array = file ("text.txt"), $ array [0] is the first line of text, and so on. If you want to flip the entire array, for example:

$ Arr = array_reverse ($ array );

The last line of text is $ arr [0].

To obtain the number of rows in the array $ array, use $ num = count ($ array );

To judge whether the text database is not empty, use: if ($ num> 0 ){}

Total number of calculated pages: $ total = ceil ($ num/$ pagesize) $ pagesize indicates the number of displayed pages.

Calculate the first record number displayed on the current page: $ number = ($ page-1) * $ pagesize;

Use "" as the separator to separate the data of every $ number unit in the array $ arr, and assign the data to the array $ rom

For ($ I = 0; $ I <= $ pagesize-1; $ I ++) {$ row = explode ("", $ arr [$ number];}

Assign the unit data in the data row to the variables in the brackets in order.

List ($ ip, $ datetime, $ name, $ email, $ home) = $ row


Bytes (file name, mode); mode: r: Read-only r +: read/write w: Write-only. Open and clear the file content. If the file does not exist, create the file. W +: read/write. Open and clear...

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.