PHP three ways to read the contents of a file (go)
Share the three ways PHP reads the contents of a file.
PHP Read File contents:
- First read mode *****************************
- Header ("Content-type:text/html;charset=utf-8");
- File path
- $file _path="Text.txt";
- Determine if you have this file
- if (file_exists ($file _path)) {
- if ($fp =fopen ($file _path,"A +")) {
- Read file
- $conn =fread ($fp,filesize ($file _path));
- Replace string
- $conn =str_replace ("\ r \ n","
",$conn);
- Echo $conn."
";
- }else{
- echo "file is not open";
- }
- }else{
- echo "does not have this file";
- }
- Fclose ($fp);
- Second Read mode ***************************
- 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);
- Third read mode, loop read *****************
- Header ("Content-type:text/html;charset=utf-8");
- File path
- $file _path="Text.txt";
- Determine if a file exists
- if (file_exists ($file _path)) {
- Determine if the file can be opened
- if ($fp =fopen ($file _path,"A +")) {
- $buffer = 1024;
- Read the side of the file to determine whether the end
- $str ="";
- while (! Feof ($fp)) {
- $str. =fread ($fp,$buffer);
- }
- }else{
- echo "File cannot be opened";
- }
- }else{
- echo "does not have this file";
- }
- Replace character
- $str =str_replace ("\ r \ n","
",$str);
- echo $str;
- Fclose ($fp);
- Functions to read the INI configuration file:
- $arr =parse_ini_file ("Config.ini");
- Returned is an array of
- echo $arr [' Host '].
";
- echo $arr [' username ']."
";
- echo $arr [' Password ']."
";