Home > Backend Development > PHP Tutorial > Three ways to read file content in PHP (transfer), _PHP tutorial

Three ways to read file content in PHP (transfer), _PHP tutorial

WBOY
Release: 2016-07-12 09:03:09
Original
948 people have browsed it

Three ways to read file content in php (transfer),

Share the three ways to read file content in php.

PHP reads file content:
  1. //**************The first reading method********************** ******* 
  2. header("content-type:text/html;charset=utf-8");
  3. //File path
  4. $file_path="text.txt";
  5. //Determine whether this file exists
  6. if(file_exists($file_path)){
  7. if($fp=fopen($file_path,"a ")){
  8. //Read file
  9. $conn=fread($fp,filesize($file_path)); > //Replace string
  10. $conn=
  11. str_replace("rn","
    ",
  12. $conn); 
  13. echo $conn."
    ";
  14. }else{ 
  15. echo
  16. "File cannot be opened";
  17. } }else{ 
  18. echo
  19. "No such file";
  20. } fclose($fp);
  21. //*******************Second reading method************************ ********** 
  22. header(
  23. "content-type:text/html;charset=utf-8");
  24. //File path
  25. $file_path=
  26. "text.txt";
  27. $conn=file_get_contents($file_path);
  28. $conn=str_replace("rn","
    ",
  29. file_get_contents(
  30. $file_path)); 🎜> echo $conn; fclose(
  31. $fp);
  32. //******************The third reading method, loop reading****************** ***
  33. header(
  34. "content-type:text/html;charset=utf-8");
  35. //File path
  36. $file_path="text.txt";
  37. //Determine whether the file exists
  38. if(file_exists(
  39. $file_path)){
  40. //Determine whether the file can be opened if($fp=
  41. fopen(
  42. $file_path,"a ")){
  43. $buffer=1024; //Judge whether you have reached the end of the file while reading
  44. $str="";
  45. while(!
  46. feof($fp)){ 
  47. $str.=fread($fp,$buffer); }
  48. }else{ 
  49. echo "File cannot be opened";
  50. }
  51. }else{ 
  52. echo "No such file";
  53. }
  54. //Replacement characters
  55. $str=str_replace("rn","
    ",$str);
    echo
    $str;
  56. fclose($fp);
  57. Function to read INI configuration file:
  58. $arr=
  59. parse_ini_file(
  60. "config.ini");
  61. //returns an array
  62. echo $arr[
  63. 'host'].
  64. "
    "; echo $arr[
  65. 'username'].
  66. "
    "; echo $arr[
  67. 'password'].
  68. "
    "; http://www.bkjia.com/PHPjc/1084377.html
  69. www.bkjia.com

true

http: //www.bkjia.com/PHPjc/1084377.html

TechArticleThree ways to read file content in php (reposted), share the three ways to read file content in php . PHP reads file content: //****************The first reading method************************ ****...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template