PHP Learning Notes-Advanced tutorials-read files, create files, write files

Source: Internet
Author: User
Tags fread
PHP Learning Notes-Advanced tutorials-read files, create files, write files

Open File: Fopen:fopen (Filename,mode);//fopen ("Test.txt", "R") , open mode: R read-only open, pointer to file header

r+ read-write mode open, point the file pointer to the file header

W write mode, point to the file header, if it does not exist, try to create

w+ read-write mode, point to the file header, if it does not exist, try to create

A write method opens, pointing to the end of the file, and if it does not exist, try to create

A + read-write mode opens, pointing to the end of the file and attempting to create if it does not exist

Read file: Fread:fread ();


ReadFile (filename): reads the contents of the file and writes it to the output buffer

 
  

fopen (Filename,mode): Open file, create file

 
  
Fopen is also used to create files, if a file is opened that does not exist, this function creates a file that the Jiading file is opened for writing (W) or added (a).
Mode Description
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.

Fwrite (resources, content) Write file contents

 
  

Fread (): The function reads the open file.

Fread ($myfile, FileSize ("Webdictionary.txt"));


Fget (resource, length) Get the contents of the file, If the length is 10, you can get 9-bit

Fgets (): Used to read a single line, fgets (file,lenght), lenght optional, specifies the number of bytes to read. The default is 1024 bytes.

Reads a row from the file pointed to by the files and returns a string up to length-1 bytes. Stop after encountering a newline character (included in the return value), EOF, or having read length -1 bytes (see first). If lengthis not specified, the default is 1 K, or 1024 bytes.

If it fails, it returns false.

 
  

FGETC (): reads a single character


Fclose (Resources) Close an open file resource

Unlink (file) Delete a file


Feof (): Check whether the end has been reached. useful for traversing data of unknown length.

 
  The output line is until End-of-filewhile (!feof ($myfile)) {  echo fgets ($myfile). "
";} Fclose ($myfile);? >


FileSize (filename): filename is a file name and is The string type. The result of this function is cached, and the Clearstatcache () is cleared to clear the cache.

FileType ();

  • 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.