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

WBOY
Release: 2016-07-13 10:39:45
Original
884 people have browsed it

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));
  10. //Replacement string
  11. $conn=str_replace("rn","
    "
    ,$conn); 
  12. echo $conn."
    "
    ;
  13. }else
  14. echo "The file cannot be opened"
  15. }else
  16. echo "No such file";
  17. fclose($fp);
  18. //************************Second reading method****************** *************
  19. header("content-type:text/html;charset=utf-8"); 
  20. //File path
  21. $file_path="text.txt"
  22. $conn=file_get_contents($file_path); >
  23. $conn=str_replace("rn","
    ",file_get_contents($file_path));
  24. echo $conn
  25. fclose($fp);
  26. //******************The third reading method, loop reading****************** ******
  27. header("content-type:text/html;charset=utf-8"); 
  28. //File path
  29. $file_path="text.txt"
  30. //Determine whether the file exists
  31. if(file_exists($file_path)){ 
  32. //Determine whether the file can be opened
  33. if($fp=fopen($file_path,"a+")){ 
  34. $buffer=1024;
  35. //Judge whether you have reached the end of the file while reading
  36. $str=""
  37. while(!feof($fp)){ 
  38. $str.=fread($fp,$buffer);
  39. }else
  40. echo "File cannot be opened";
  41. }else
  42. echo "No such file";
  43. //Replacement characters
  44. $str=str_replace("rn","
    "
    ,$str); 
  45. echo $str
  46. fclose($fp);
  47. Function to read INI configuration file:
  48. $arr=parse_ini_file("config.ini") ; 
  49. //Returns an array
  50. echo $arr['host']."
    "
  51. echo $arr['username']."
    "
  52. echo $arr['password']."
    "

Reference link:

Code for php to read file content into string and process it
Learn how to read file content in php

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/729832.htmlTechArticlephp reads file content: //**************First A reading method**************************** header( "content-type:text/html;charset=utf-8" ); //File path $file_path = "text.txt" ;...
Related labels:
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