Several Methods for php to read files

Source: Internet
Author: User
File Operation steps: open, operate, and close. $ Fopenfopen (path, method), fwrite ($ fopen, Written string), and fclose ($ fopen ). The Mode description is r-read-only. Start at the beginning of the file. R + read/write. Start at the beginning of the file. W write only. Open and clear the file content.

File Operation steps: open, operate, and close. $ Fopen = fopen (path, method), fwrite ($ fopen, Written string), and fclose ($ fopen ). The Mode description is r-read-only. Start at the beginning of the file. R + read/write. Start at the beginning of the file. W write only. Open and clear the file content.

File Operation steps: open, operate, and close. $ Fopen = fopen (path, method), fwrite ($ fopen, Written string), and fclose ($ fopen ).

The following methods can be used to open a file:

Mode Description
R Read-only. Start at the beginning of the file.
R + Read/write. Start at the beginning of the file.
W Write-only. Open and clear the file content. If the file does not exist, create a new file.
W + Read/write. Open and clear the file content. If the file does not exist, create a new file.
A Append. Open and write to the end of the file. If the file does not exist, create a new file.
A + Read/append. Write content to the end of the file to keep the file content.
X Write-only. Create a new file. If the file already exists, FALSE is returned.
X +

Read/write. Create a new file. If the file already exists, FALSE and an error are returned.

NOTE: If fopen () cannot open the specified file, 0 (false) is returned ).

The following describes how to read a file:

1. open the file to read

1. readfile ()

Readfile ("file1.txt ");

2. file ()

$ F_arr = file ("file1.txt ");
Foreach ($ f_arr as $ content)
{
Echo $ content ."
";
}

3. file_get_contents ()

$ F_chr = file_get_contents ("file1.txt ");
Echo $ f_chr;

Ii. open the file to read

1. fgets ()

$ Fopen = fopen ("file1.txt", "rb ");
While (! Feof ($ fopen) // function test whether the pointer has reached the end of the file
{
Echo fgets ($ fopen); // output the current row
}
Fclose ($ fopen );

2. fgetss ()

$ Fopen = fopen ("file.txt", "rb ");
While (! Feof ($ fopen) // function test whether the pointer has reached the end of the file
{
Echo fgetss ($ fopen); // output the current row
}
Fclose ($ fopen );

3. fgetc ()

$ Fopen = fopen ("file1.txt", "rb"); // open the file stream
While (false! ==( $ Chr = fgetc ($ fopen) // gets a string and determines whether it is false.
{// If not, output this character
Echo $ chr;
}
Fclose ($ fopen); // close file resources

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.