PHP Three modes of reading the contents of a file (RPM)

Source: Internet
Author: User
Tags fread php read file vars
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:
  1. First read mode *****************************
  2. Header ("Content-type:text/html;charset=utf-8");
  3. File path
  4. $file _path="Text.txt";
  5. Determine if you have this file
  6. if (file_exists ($file _path)) {
  7. if ($fp =fopen ($file _path,"A +")) {
  8. Read file
  9. $conn =fread ($fp,filesize ($file _path));
  10. Replace string
  11. $conn =str_replace ("\ r \ n","
    ",$conn);
  12. Echo $conn."
    ";
  13. }else{
  14. echo "file is not open";
  15. }
  16. }else{
  17. echo "does not have this file";
  18. }
  19. Fclose ($fp);
  20. Second Read mode ***************************
  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. Third read mode, loop read *****************
  29. Header ("Content-type:text/html;charset=utf-8");
  30. File path
  31. $file _path="Text.txt";
  32. Determine if a file exists
  33. if (file_exists ($file _path)) {
  34. Determine if the file can be opened
  35. if ($fp =fopen ($file _path,"A +")) {
  36. $buffer = 1024;
  37. Read the side of the file to determine whether the end
  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 "does not have this file";
  47. }
  48. Replace character
  49. $str =str_replace ("\ r \ n","
    ",$str);
  50. echo $str;
  51. Fclose ($fp);
  52. Functions to read the INI configuration file:
  53. $arr =parse_ini_file ("Config.ini");
  54. Returned is an array of
  55. echo $arr [' Host '].
    ";
  56. echo $arr [' username ']."
    ";
  57. echo $arr [' Password ']."
    ";

  • 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.