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

Source: Internet
Author: User
Tags fread readfile

Open File: Fopen:fopen (Filename,mode);//fopen ("Test.txt", "R");Open mode: R read-only open, the file pointer to the file header r+ read-write mode open, the file pointer to the file header W write method, point to the file header, if not present, try to create w+ Read-write mode, point to the file header, if it does not exist then try to create a write mode open, point to the end of the file, if not exist then try to create a + read-write mode open, point to the end of the file, if not present, try to create read file: Fread:fre AD ();
ReadFile (filename): reads the contents of the file and writes it to the output buffer
<?phpecho ReadFile ("Webdictionary.txt");? >
fopen (Filename,mode): Open file, create file
<?php$myfile = fopen ("Webdictionary.txt", "R") or Die ("Unable to open file!"); Echo fread ($myfile, FileSize ("Webdictionary.txt")); fclose ($myfile);? >
Fopen is also used to create files, assuming open files that do not exist, this function creates a file, the Jiading file is opened for writing (W) or added (a).
Mode Descriptive narrative
R Open the file as read only. The file pointer starts at the beginning of the file.
W Open the file for just write. Delete the contents of the file or create a new file, assuming it does not exist. The file pointer starts at the beginning of the file.
A Open the file for just write. The existing data in the file will be retained. The file pointer starts at the end of the file. Create a new file, assuming the file does not exist.
X Create a new file for just write. Returns false and an error, assuming 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, assuming 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. Create a new file, assuming it does not exist.
x+ Create a new file for read/write. Returns false and an error, assuming the file already exists.
Fwrite (resources, content) Write file contents
<?php$myfile = fopen ("Newfile.txt", "w") or Die ("Unable to open file!"); $txt = "Bill gates\n"; fwrite ($myfile, $txt); $txt = "Steve jobs\n"; fwrite ($myfile, $txt); fclose ($myfile);? >
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-bitFgets (): 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). Assuming that lengthis not specified, the default is 1K, or 1024 bytes.

If it fails, it returns false.

<?php$myfile = fopen ("Webdictionary.txt", "R") or Die ("Unable to open file!"); Echo fgets ($myfile); fclose ($myfile);? >
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.is useful for traversing data with unknown lengths.
<?php$myfile = fopen ("Webdictionary.txt", "R") or Die ("Unable to open file!"); //Output single line until End-of-filewhile (!feof ($myfile)) {  echo fgets ($myfile). "<br>";} Fclose ($myfile);? >

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

PHP Learning Notes-Advanced tutorials-read files, create files, write 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.