Example of three feasible methods for php to read file content _ PHP-php Tutorial
Source: Internet
Author: User
This article mainly introduces three methods for php to read file content. you can refer to the following three methods for php to read file content:
// ************** First method of reading ******************** *********
The code is as follows:
Header ("content-type: text/html; charset = utf-8 ");
// File path
$ File_path = "text.txt ";
// Determine whether the file exists
If (file_exists ($ file_path )){
If ($ fp = fopen ($ file_path, "a + ")){
// Read the file
$ Conn = fread ($ fp, filesize ($ file_path ));
// Replace the string
$ Conn = str_replace ("\ r \ n ","
", $ Conn );
Echo $ conn ."
";
} Else {
Echo "file cannot be opened ";
}
} Else {
Echo "this file does not exist ";
}
Fclose ($ fp );
// ******************* Second read method *************** ************
The code is as follows:
Header ("content-type: text/html; charset = utf-8 ");
// File path
$ File_path = "text.txt ";
$ Conn = file_get_contents ($ file_path );
$ Conn = str_replace ("\ r \ n ","
", File_get_contents ($ file_path ));
Echo $ conn;
Fclose ($ fp );
// ******************** The third read method, cyclically read *****************
The code is as follows:
Header ("content-type: text/html; charset = utf-8 ");
// File path
$ File_path = "text.txt ";
// Determine whether a file exists
If (file_exists ($ file_path )){
// Determine whether the file can be opened
If ($ fp = fopen ($ file_path, "a + ")){
$ Buffer = 1024;
// Read and judge whether the object ends
$ Str = "";
While (! Feof ($ fp )){
$ Str. = fread ($ fp, $ buffer );
}
} Else {
Echo "file cannot be opened ";
}
} Else {
Echo "this file does not exist ";
}
// Replace the character
$ Str = str_replace ("\ r \ n ","
", $ Str );
Echo $ str;
Fclose ($ fp );
Functions used to read the INI configuration file:
$ Arr = parse_ini_file ("config. ini ");
// The returned result is an array.
Echo $ arr ['host']."
";
Echo $ arr ['username']."
";
Echo $ arr ['password']."
";
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.