Basic operations on php files

Source: Internet
Author: User
Tags rewind ftp protocol
Basic operations on php files

  1. // Basic file operations
  2. // Fopen ()
  3. Open a file
  4. // Open a file in the absolute path, select read-only mode, and return the resource $ handle
  5. $ Handle = fopen ("D:/lamp/apache2/htdocs/test/file.txt", "r ");
  6. // Access the file in the root directory of the document and select the read-only mode
  7. $ Handle = fopen ($ _ SERVER ['document _ root']. "/test/file.txt", "r ");
  8. // Open a remote file. the http protocol can only be opened in read mode.
  9. $ Handle = fopen ("http://www.wowsai.com", "r ");
  10. // Use the FTP protocol to open a remote file. if the FTP server is writable, it can be opened in write mode.
  11. // $ Handle = fopen ("ftp: // user: password@example.com/file.txt", 'w ');
  12. // Fclose ()
  13. Disable open resource type
  14. // Fwrite ()
  15. Write content to the file
  16. $ FileName = "data.txt ";
  17. // Declare the variable of a file
  18. // Open the file in write-only mode. If no file exists, create the file and use the program when opening 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 the file through a loop
  22. Fwrite ($ f_handle, "again \ n ");
  23. }
  24. Fclose ($ f_handle );
  25. // Close the opened file
  26. // File_put_contents ()
  27. Write all data to the specified file at a time
  28. // Read the file content
  29. // Fread ()
  30. Read open files
  31. // File_get_contents ()
  32. Read a file into a string
  33. // Fgets ()
  34. Returns a row from an opened file.
  35. // Fgetc ()
  36. Returns characters from an opened file.
  37. // File ()
  38. Read the 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 located at the end of the file.
  43. // Read the specified number of bytes from the file and save it to a variable.
  44. $ FileName2 = "data.txt ";
  45. $ F_hand = fopen ($ fileName2, "r") or die ("file opening failed ");
  46. $ Contents = fread ($ f_hand, 50 );
  47. Fclose ($ f_hand );
  48. Echo $ contents ."
    ";
  49. // Read all the content from the file and store it into a variable. each time you read a part, read it cyclically.
  50. /* $ FileName3 = "D:/lamp/apache2/icons/link.gif ";
  51. // Save the file name of the binary file to a variable
  52. $ F3_handle = fopen ($ fileName3, "rb") or die ("file opening failed"); // open the file in read-only mode, and add "B"
  53. $ F3_contents = "";
  54. // Declare a string used to save the file content
  55. While (! Feof ($ f3_handle )){
  56. // Read the content of the file cyclically and know the end of the file
  57. $ F3_contents. = fread ($ f3_handle, 1024 );
  58. // Read 1024 characters each time
  59. }
  60. Fclose ($ f3_handle );
  61. Echo $ f3_contents ;*/
  62. // Another method for reading all the content of a file
  63. $ FileName4 = "data.txt ";
  64. $ F4_handle = fopen ($ fileName4, "r") or die ("file opening failed ");
  65. $ F4_con = fread ($ f4_handle, filesize ($ fileName4 ));
  66. // Use filesize to get the object length, so as to read all the object content
  67. Fclose ($ f4_handle );
  68. Echo $ f4_con ."
    ";
  69. // Another method is to read all the content of a file, which is much better than the above method.
  70. Echo file_get_contents ("data.txt ");
  71. $ F5_handle = fopen ("data.txt", "r") or die ("file opening failed ");
  72. While (! Feof ($ f5_handle )){
  73. // Determine whether the pointer ends with the end of the file
  74. $ Buffer = fgets ($ f5_handle );
  75. // Read a row from the file each time
  76. Echo $ buffer ."
    ";
  77. }
  78. Fclose ($ f5_handle );
  79. $ F6_handle = fopen ("data.txt", "r") or die ("file opening failed ");
  80. While (! Feof ($ f6_handle )){
  81. // Determine whether the pointer ends with the end of the file
  82. $ Buffer = fgetc ($ f6_handle );
  83. // Read one character from the object each time
  84. Echo $ buffer ."
    ";
  85. }
  86. Fclose ($ f6_handle );
  87. Print_r (file ("data.txt "));
  88. // Read the file into an array
  89. Readfile ("data.txt ");
  90. // Read the content of the file and output it to the browser.
  91. // Access a remote file
  92. Make sure that "allow_url_fopen" in php. ini is enabled and that the remote file has access permissions.
  93. $ Ws_file = fopen ("http://www.wowsai.com", "r") or die ("remote file opening 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 the regular expression to match the website title
  99. $ Title = $ res [1];
  100. Break;
  101. }
  102. }
  103. Fclose ($ ws_file );
  104. Echo $ title ."
    ";
  105. // Move the object pointer
  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 opening failed ");
  113. // Open the file in read-only mode
  114. Echo ftell ($ fp )."
    ";
  115. // Output the pointer position when the file is opened. the default value is 0.
  116. Echo fread ($ fp, 10 )."
    ";
  117. // Read the first 10 characters of the file, and the file pointer has changed.
  118. Echo ftell ($ fp )."
    ";
  119. // After reading the file with 10 characters, the pointer of the file reaches the 10 position
  120. Fseek ($ fp, 30, SEEK_CUR );
  121. // Move the object pointer 30 characters backward
  122. Echo ftell ($ fp )."
    ";
  123. // 30 characters after the file is moved, it reaches the position of 40.
  124. Echo fread ($ fp, 10 )."
    ";
  125. // Read the characters between 40 and 50, and the pointer will reach 50
  126. Fseek ($ fp,-10, SEEK_END );
  127. // Set the pointer to the last 10th position of the file
  128. Echo fread ($ fp, 10 )."
    ";
  129. // Output the last 10 characters of the file
  130. Rewind ($ fp );
  131. // Set the file pointer to the beginning of the file
  132. Echo ftell ($ fp );
  133. // The pointer of the file returns to the beginning, so 0 is output.
  134. Fclose ($ fp );
  135. ?>


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.