Chapter II Storage and retrieval of data (2)

Source: Internet
Author: User
Tags flock rewind

Focus:

    • Know when to finish reading files: feof () function
    • Reads one row of data at a time: fgets (), FGETSS (), fgetcsv () functions
    • Read entire file: ReadFile (), Fpassthru (), file () function
    • Read one character: Fgetc () function
    • Read any length: Fread () function
    • To see if a file exists: File_exists () function
    • Determine file Size: FileSize () function
    • Delete a file: Unlink () function
    • Locate in File: Rewind (), fseek (), Ftell () function
    • File Lock: Flock () function (previously spoken)

An example of reading a file first: program manifest 2-3 vieworder.php--the employee interface used to view the order file

<?PHP//Create short variable name$DOCUMENT _root=$_server[' Document_root '];?>s Auto parts-customer orders</title>' s Auto partsPHP//Open the file (orders.txt); "RB" means "only can read"@$fp=fopen("$DOCUMENT _root/.. /orders.txt ", ' RB '); //lock file for reading    Flock($fp,lock_sh); //judge the file is full or not    if(!$fp){        Echo"<p><strong>no orders pending. Please try again later.</strong></p> "; Exit; }    //judge when the file go to the end     while(!feof($fp)){        er=fgets($fp, 999); //Read one line of content from the this file each time , it would stop when counter a "\ n" or "EOF" or 998B        Echo er." <br/> "; }    Echo"Final position of the file pointer is:". (Ftell($fp)); Echo"<br/>"; Rewind($fp); Echo"After rewind, the position is:". (Ftell($fp)); Echo"<br/>"; //Release the file    Flock($fp,Lock_un); fclose($fp); ?></body>View Code

Then I go through the line analysis:

    while (!feof ($fp)) {        er = fgets ($fp, 999);        Read one line of content from the this file each time, it would stop when counter a "\ n" or "EOF" or 998B        echo er. " <br/> ";    }

This uses the Fgets () function, which reads one row of data at a time, and the feof () function, which functions as "judging when to read a file".

First, the meaning of this code: the use of a while loop, when the file pointer FP does not reach the end of the file, each time a row is read, when the file pointer reads "\ n"/"EOF"/998b, a line break is printed on the vieworder.php interface.

A little tip:

        • The Web browser does not represent spaces, such as Sia, so we'd better replace the line terminator in the text with the newline character in HTML <br/>.
        • 999: Here is the maximum readable length for the specified length (we ourselves as per the actual conditions) minus 1B, i.e. 998B.
    • Feof () function: Test If the file pointer is at the end of the file--bool feof (Resource $handle)

Handle: Represents a file pointer, in this case the handle is "$fp".

If the file pointer points to the end of the file, the feof () function returns True.

Foef ==file End of File.

    • Reads one row of data at a time: fgets (), FGETSS (), fgetcsv () functions
        • Fgets (): reads a line of content from the file pointer every time--string fgets (Resource $handle [, int $length])

Handle: The file pointer is required and must point to a file that was successfully opened by fopen () or Fsockopen () (and not yet closed by fclose ())

Length: Reads a row from the file pointed to by handle and returns a string of up to length-1 bytes in length. Encounters a newline character (included in the return value), EOF, or

The length-1 byte has been read and stopped (see what happens first). If length is not specified, The default is 1 K, or 1024 bytes.

        • FGETSS (): Reads a row from the file pointer and filters out the HTML tag--string fgets (Resource $handle [, int $length [, String $allowable _tags]])

Allowable_tags: Optional third parameter specifies which tags are not removed

      • Fgetcsv (): reads a line from the file pointer and resolves the CSV field--ArrayFgetcsv(Resource$handle[, Int$length= 0[, string  $delimiter  = ', '  [,  string $enclosure  = ' "'  [, string  $escape  = ' \ \ ' &NBSP;]]])

Delimiter: Set field delimiter (only one character allowed)

Enclosure: Set field wrapping character (only one characters allowed)

Escape: Set escape character (only one character allowed), default is a backslash

Returns an indexed array containing the Read fields

 

    echo "Final position of the file pointer is:". (Ftell ($FP));    echo "<br/>";    Rewind ($FP);    echo "After rewind, the position is:". (Ftell ($FP));    echo "<br/>";

The Ftell () function and the rewind () function, which functions as "Locating in Files" are used here,

First of all, the meaning of this code: print out the last position of the file pointer----wrap--Reset the file pointer to the beginning of the file--after the print reset, the position of the pointer--newline

  • Locate in File: Rewind (), fseek (), Ftell () function
      • Rewind (): Rewind the position of the file pointer (the file location that will be handle is set to the beginning of the file stream)--bool Rewind (Resource $handle)
      • Fseek (): Locating in the file pointer--Int  fseek   (  resource  $handle Span class= "Apple-converted-space" > , < Span class= "type" >int  $offset  [,  int  $whence  = seek_set  ])

< Span class= "Apple-converted-space" > < Span class= "Apple-converted-space" > offest: Offset. To move to the position before the end of the file, you need to pass a negative value to offset and set whence to Seek_end.

< Span class= "Apple-converted-space" > < Span class= "Apple-converted-space" > whence:

< Span class= "Apple-converted-space" > < Span class= "Apple-converted-space" >" 1 "seek_set-Set position equals offset byte / span>

< Span class= "Apple-converted-space" > < Span class= "Apple-converted-space" >" 2 "seek_cur-set position for current position plus offset

< Span class= "Apple-converted-space" > < Span class= "Apple-converted-space" >" 3 "seek_end-set location for end of file plus offest /span>

      • < Span class= "Apple-converted-space" >ftell (): Returns the position of the file pointer read/write--int < Span class= "methodname" > ftell   ( resource   $handle  )            

            

Chapter II Storage and retrieval of data (2)

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.