Basic operation of PHP files

Source: Internet
Author: User
Tags fread readfile rewind ftp protocol
  1. Basic operation of the file
  2. fopen ()
  3. Open a file
  4. Open a file with an absolute path, select Read-only mode, and return the resource $handle
  5. $handle =fopen ("D:/lamp/apache2/htdocs/test/file.txt", "R");
  6. Access files under the root of the document, and also select Read-only mode
  7. $handle =fopen ($_server[' document_root '). " /test/file.txt "," R ");
  8. Open a remote file and use the HTTP protocol to open it in a read-only manner
  9. $handle =fopen ("http://www.wowsai.com", "R");
  10. Open a remote file using the FTP protocol, if the FTP server is writable, you can open it in write mode
  11. $handle =fopen ("Ftp://user:password@example.com/file.txt", ' W ');
  12. Fclose ()
  13. To close an open resource type
  14. Fwrite ()
  15. Writing content to a file
  16. $fileName = "Data.txt";
  17. Declaring a variable for a file
  18. Open a file in a write-only manner, not created, and passed through the program when the open fails
  19. $f _handle=fopen ("Data.txt", "w") or Die ("open ". $fileName. " File failed ");
  20. for ($i =0; $i <10; $i + +) {
  21. Add content to a file through a loop
  22. Fwrite ($f _handle, "again\n");
  23. }
  24. Fclose ($f _handle);
  25. Close an open file
  26. File_put_contents ()
  27. Writes all data to the specified file at once
  28. Read File contents
  29. Fread ()
  30. Read an open file
  31. File_get_contents ()
  32. To read a file into a string
  33. Fgets ()
  34. Returns a row from an open file
  35. FGETC ()
  36. Returning characters from an open file
  37. File ()
  38. To read a file into an array
  39. ReadFile ()
  40. Reads a file and outputs it to the output buffer
  41. Feof ()
  42. Determines whether a file pointer is at the end of the file
  43. Reads the contents of a specified number of bytes from a file into a variable
  44. $fileName 2= "Data.txt";
  45. $f _hand=fopen ($fileName 2, "R") or Die ("File open failed");
  46. $contents =fread ($f _hand,50);
  47. Fclose ($f _hand);
  48. echo $contents. "
    ";
  49. Reads everything from a file into a variable, reads one part at a time, loops through
  50. /* $fileName 3= "d:/lamp/apache2/icons/link.gif";
  51. Save the file name of the binaries in a variable
  52. $f 3_handle=fopen ($fileName 3, "RB") or Die ("File open failed"); Open the file in read-only mode with "B" added
  53. $f 3_contents= "";
  54. Declares a string to hold the contents of the file
  55. while (!feof ($f 3_handle)) {
  56. Loop through the contents of the file, knowing the end of the file
  57. $f 3_contents.=fread ($f 3_handle,1024);
  58. Reads 1024 characters at a time
  59. }
  60. Fclose ($f 3_handle);
  61. echo $f 3_contents; */
  62. Another way to read the entire contents of a file
  63. $fileName 4= "Data.txt";
  64. $f 4_handle=fopen ($fileName 4, "R") or Die ("File open failed");
  65. $f 4_con=fread ($f 4_handle,filesize ($fileName 4));
  66. Use FileSize to get the length of the file so that it reads the entire contents of the file
  67. Fclose ($f 4_handle);
  68. echo $f 4_con. "
    ";
  69. Another way to read the entire contents of a file is much better than the performance above
  70. Echo file_get_contents ("Data.txt");
  71. $f 5_handle=fopen ("Data.txt", "R") or Die ("File open failed");
  72. while (!feof ($f 5_handle)) {
  73. Determine if the pointer is to the end of the file
  74. $buffer =fgets ($f 5_handle);
  75. Every time a row is read from a file
  76. echo $buffer. "
    ";
  77. }
  78. Fclose ($f 5_handle);
  79. $f 6_handle=fopen ("Data.txt", "R") or Die ("File open failed");
  80. while (!feof ($f 6_handle)) {
  81. Determine if the pointer is to the end of the file
  82. $buffer =fgetc ($f 6_handle);
  83. Each time a character is read from a file
  84. echo $buffer. "
    ";
  85. }
  86. Fclose ($f 6_handle);
  87. Print_r (File ("Data.txt"));
  88. Read the file into an array
  89. ReadFile ("Data.txt");
  90. Read the contents of the file directly and output it to the browser
  91. accessing remote Files
  92. Ensure that "Allow_url_fopen" in PHP.ini is turned on and that the remote file has access rights
  93. $ws _file=fopen ("http://www.wowsai.com", "R") or Die ("Remote File Open failed");
  94. Open a remote file
  95. while (!feof ($ws _file)) {
  96. $ws _line=fgets ($ws _file);
  97. if (Preg_match ("/(. *) <\/title>/", $ws _line, $res)) {
  98. Use regular match titles for Web sites
  99. $title = $res [1];
  100. Break
  101. }
  102. }
  103. Fclose ($ws _file);
  104. echo $title. "
    ";
  105. Move a pointer to a file
  106. Ftell ()
  107. Returns the current position of the pointer
  108. Fseek ()
  109. Move the pointer to the specified position
  110. Rewind ()
  111. Move the pointer to the beginning of the file
  112. $FP =fopen ("Data.txt", "R") or Die ("File open failed");
  113. Open a file with read-only mode
  114. Echo Ftell ($fp). "
    ";
  115. Outputs the position of the pointer when the file was just opened, default is 0
  116. Echo fread ($FP, 10). "
    ";
  117. Read the first 10 characters of a file, the file pointer has changed
  118. Echo Ftell ($fp). "
    ";
  119. After reading 10 characters of the file, the pointer to the file is in the 10 position
  120. Fseek ($fp, 30,seek_cur);
  121. Move the pointer of a file backwards by 30 characters
  122. Echo Ftell ($fp). "
    ";
  123. The file moved 30 characters, to the 40 position
  124. Echo fread ($FP, 10). "
    ";
  125. Reads a character from 40 to 50, and the pointer moves to 50
  126. Fseek ($fp, -10,seek_end);
  127. Set the pointer to the 10th of the file's penultimate position
  128. Echo fread ($FP, 10). "
    ";
  129. The last 10 characters of the output file
  130. Rewind ($FP);
  131. Set the file pointer to the beginning of the file
  132. Echo Ftell ($FP);
  133. The pointer to the file goes back to the beginning, so it outputs 0
  134. Fclose ($FP);
  135. ?>
Copy Code
Php
  • 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.