PHP operation files

Source: Internet
Author: User
PHP operation files
This class is a file operation class that implements file creation, writing, deletion, modification, copying, moving, creating directories, and deleting directories.

  1. /**
  2. * This class is a file operation class that allows you to create, write, delete, modify, copy, move, create, and delete files.
  3. * List files and other functions in the directory. do not forget to add "/" to the path "/"
  4. *
  5. * @ Author passers-by Hao
  6. * @ Copyright myself
  7. * @ Link www.phpr.cn
  8. *
  9. */
  10. Class fileoperate
  11. {
  12. Var path; // file path
  13. Var name; // file name
  14. Var result; // The result after the file operation
  15. /**
  16. * This method is used to create a name file in the path directory.
  17. *
  18. * @ Param string path
  19. * @ Param string name
  20. */
  21. Function creat_file (path, name) // create a file
  22. {
  23. Filename = path. name;
  24. If (file_exists (filename ))
  25. {
  26. Echo "the file already exists. please change the file name ";
  27. }
  28. Else
  29. {
  30. If (file_exists (path ))
  31. {
  32. Touch (name );
  33. Rename (name, filename );
  34. Echo "file created successfully
    ";
  35. }
  36. Else {
  37. Echo "the directory does not exist. check ";
  38. }
  39. }
  40. }
  41. /**
  42. * This method is used to write files and write content to the name file in the path. bool is the write option. if the value is 1
  43. * Write the original content of the file. if the value is 2, only the content of this file is written.
  44. *
  45. * @ Param string_type path
  46. * @ Param string_type name
  47. * @ Param string_type content
  48. * @ Param bool_type bool
  49. */
  50. Function write_file (path, name, content, bool) // write a file
  51. {
  52. Filename = path. name;
  53. If (bool = 1 ){
  54. If (is_writable (filename )){
  55. Handle = fopen (filename, 'A ');
  56. If (! Handle ){
  57. Echo "the file cannot be opened or the file does not exist ";
  58. Exit;
  59. }
  60. Result = fwrite (handle, content );
  61. If (! Result ){
  62. Echo "file writing failed ";
  63. }
  64. Echo "file written successfully ";
  65. Fclose (handle );
  66. }
  67. Else echo "the file does not exist ";
  68. }
  69. If (bool = 2 ){
  70. If (! File_exists (filename )){
  71. This-> creat_file (path, name );
  72. Handle = fopen (filename, 'A ');
  73. If (fwrite (handle, content ));
  74. Echo "file written successfully ";
  75. }
  76. Else {
  77. Unlink (filename );
  78. This-> creat_file (path, name );
  79. This-> write_file (path, name, content, 1 );
  80. Echo "file modified ";
  81. }
  82. }
  83. }
  84. /**
  85. * This method deletes the name file in the path.
  86. *
  87. * @ Param string_type path
  88. * @ Param string_type name
  89. */
  90. Function del_file (path, name) {// delete an object
  91. Filename = path. name;
  92. If (! File_exists (filename )){
  93. Echo "the file does not exist. check whether the path is correct ";
  94. }
  95. Else {
  96. If (unlink (filename )){
  97. Echo "the file is successfully deleted ";
  98. }
  99. Else echo "failed to delete the file ";
  100. }
  101. }
  102. /**
  103. * This method is used to modify the content (visible) in the name file in the path directory)
  104. *
  105. * @ Param string_type path
  106. * @ Param string_type name
  107. */
  108. Function modi_file (path, name) {// file modification
  109. Filename = path. name;
  110. Handle = fopen (filename, 'R + ');
  111. Content = file_get_contents (filename );
  112. Echo "";
  113. }
  114. /**
  115. * This method is used to copy the name file from spath to dpath.
  116. *
  117. * @ Param string name
  118. * @ Param string spath
  119. * @ Param string dpath
  120. */
  121. Function copy_file (name, spath, dpath) // copy a file
  122. {
  123. Filename = spath. name;
  124. If (file_exists (filename )){
  125. Handle = fopen (filename, 'A ');
  126. Copy (filename, dpath. name );
  127. If (file_exists (dpath. name ))
  128. Echo "file copied ";
  129. Else echo "failed to copy the file ";
  130. }
  131. Else echo "the file does not exist ";
  132. }
  133. /**
  134. * This method moves the name file from spath to the path
  135. *
  136. * @ Param string_type path
  137. * @ Param string_type dirname
  138. * @ Param string_type dpath
  139. */
  140. Function move_file (name, spath, dpath) // move the file
  141. {
  142. Filename = spath. name;
  143. If (file_exists (filename )){
  144. Result = rename (filename, dpath. name );
  145. If (result = false or! File_exists (dpath ))
  146. Echo "failed to move the file or the target directory does not exist ";
  147. Else
  148. Echo "file moved successfully ";
  149. }
  150. Else {
  151. Echo "the file does not exist and cannot be moved ";
  152. }
  153. }
  154. }
  155. ?>

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.