Php three ways to read file content) _ PHP Tutorial

Source: Internet
Author: User
Php can read file content in three ways (convert ). Php reads the file content: ********************** * ****** header (content-type: texthtml?charset=8w.w.w.w.w.w.file_pathtext.txt; php reads the file content:
  1. // ************** First method of reading ******************** *********
  2. Header ("content-type: text/html; charset = utf-8 ");
  3. // File path
  4. $ File_path = "text.txt ";
  5. // Determine whether the file exists
  6. If (file_exists ($ file_path )){
  7. If ($ fp = fopen ($ file_path, "a + ")){
  8. // Read the file
  9. $ Conn = fread ($ fp, filesize ($ file_path ));
  10. // Replace the string
  11. $ Conn = str_replace ("\ r \ n ","
    ", $ Conn );
  12. Echo $ conn ."
    ";
  13. } Else {
  14. Echo "file cannot be opened ";
  15. }
  16. } Else {
  17. Echo "this file does not exist ";
  18. }
  19. Fclose ($ fp );
  20. // ******************* Second read method *************** ************
  21. Header ("content-type: text/html; charset = utf-8 ");
  22. // File path
  23. $ File_path = "text.txt ";
  24. $ Conn = file_get_contents ($ file_path );
  25. $ Conn = str_replace ("\ r \ n ","
    ", File_get_contents ($ file_path ));
  26. Echo $ conn;
  27. Fclose ($ fp );
  28. // ******************** The third read method, cyclically read *****************
  29. Header ("content-type: text/html; charset = utf-8 ");
  30. // File path
  31. $ File_path = "text.txt ";
  32. // Determine whether a file exists
  33. If (file_exists ($ file_path )){
  34. // Determine whether the file can be opened
  35. If ($ fp = fopen ($ file_path, "a + ")){
  36. $ Buffer = 1024;
  37. // Read and judge whether the object ends
  38. $ Str = "";
  39. While (! Feof ($ fp )){
  40. $ Str. = fread ($ fp, $ buffer );
  41. }
  42. } Else {
  43. Echo "file cannot be opened ";
  44. }
  45. } Else {
  46. Echo "this file does not exist ";
  47. }
  48. // Replace the character
  49. $ Str = str_replace ("\ r \ n ","
    ", $ Str );
  50. Echo $ str;
  51. Fclose ($ fp );
  52. Functions used to read the INI configuration file:
  53. $ Arr = parse_ini_file ("config. ini ");
  54. // The returned result is an array.
  55. Echo $ arr ['host']."
    ";
  56. Echo $ arr ['username']."
    ";
  57. Echo $ arr ['password']."
    ";

Reference link:

Php code for reading and processing the file content to strings
How to Read file content in php

******************* * ********* Header ("content-type: text/html; charset = utf-8 "); // file path $ file_path =" text.txt ";...

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.