PHP Action File class

Source: Internet
Author: User
This class is the file operation class, implements the file establishment, writes, deletes, modifies, duplicates, moves, creates the directory, deletes the directory
  1. /**
  2. * This class is the file operation class, implements the file establishment, writes, deletes, modifies, copies, moves, creates the directory, deletes the directory
  3. * List the files in the directory and other functions, do not forget to add "/" after the path
  4. *
  5. * @author Lu Shin
  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;//results after file operation
  15. /**
  16. * This method is used to create the name file in the path directory
  17. *
  18. * @param string path
  19. * @param string name
  20. */
  21. function Creat_file (path,name)//Build file
  22. {
  23. Filename=path.name;
  24. if (file_exists (filename))
  25. {
  26. echo "File already exists, please change the filename";
  27. }
  28. Else
  29. {
  30. if (file_exists (path))
  31. {
  32. Touch (name);
  33. Rename (name,filename);
  34. echo "File established successfully
    ";
  35. }
  36. else{
  37. echo "Directory does not exist, please check";
  38. }
  39. }
  40. }
  41. /**
  42. * This method is used to write the file, to the path path under the name file to write content, bool is the Write option, the value is 1 o'clock
  43. * Next to the original content of the file continues to write, the value is 2 o'clock after the file is written only this content
  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 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 "file cannot be opened or the file does not exist";
  58. Exit
  59. }
  60. Result=fwrite (handle,content);
  61. if (!result) {
  62. echo "File failed to write";
  63. }
  64. echo "File write succeeded";
  65. Fclose (handle);
  66. }
  67. else echo "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 write succeeded";
  75. }
  76. else {
  77. unlink (filename);
  78. This->creat_file (Path,name);
  79. This->write_file (path,name,content,1);
  80. echo "File modified successfully";
  81. }
  82. }
  83. }
  84. /**
  85. * This method deletes the path path under the name file
  86. *
  87. * @param string_type Path
  88. * @param string_type Name
  89. */
  90. function Del_file (path,name) {//delete file
  91. Filename=path.name;
  92. if (!file_exists (filename)) {
  93. echo "File does not exist, please confirm the path is correct";
  94. }
  95. else {
  96. if (unlink (filename)) {
  97. echo "File deletion succeeded";
  98. }
  99. else echo "File deletion failed";
  100. }
  101. }
  102. /**
  103. * This method is used to modify the contents of the name file in the path directory (visual)
  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)//File copy
  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 copy succeeded";
  129. else echo "file copy failed";
  130. }
  131. else echo "file does not exist";
  132. }
  133. /**
  134. * This method moves the name file from spath to 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 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 "File move failed or destination directory does not exist";
  147. Else
  148. echo "File moved successfully";
  149. }
  150. else {
  151. echo "File does not exist, cannot be moved";
  152. }
  153. }
  154. }
  155. ?>
Copy Code
  • 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.