PHP Directory Operations

Source: Internet
Author: User
  1. BaseName ()
  2. Returns the file name portion of the path
  3. $path = "d:/lamp/apache2/htdocs/file.php";
  4. Echo basename ($path). "
    ";
  5. Display filenames with file name extensions
  6. Echo basename ($path, ' php ').
    ";
  7. Show filenames without file name extensions
  8. DirName ()
  9. Remove file name, return directory name
  10. echo dirname ($path). "
    ";
  11. Return directory Name
  12. PathInfo ()
  13. Returns an array of path properties
  14. Print_r (PathInfo ($path)). "
    ";
  15. Opendir ()
  16. Open the specified directory
  17. Readdir ()
  18. Reads the specified directory
  19. Closedir ()
  20. Close the specified directory
  21. Rewinddir ()
  22. Rewind directory Handle
  23. /*
  24. * The following code to count the files in a directory
  25. */
  26. $num = 0;
  27. Used to count the total number of subdirectories and files
  28. $dirname = "PM3";
  29. Define a directory, that is, the directory to traverse
  30. $dir _handle=opendir ($dirname);
  31. Open Directory
  32. Output traversed content and file names using tabular format
  33. echo "
  34. echo "
  35. echo "
  36. echo "
  37. while ($file =readdir ($dir _handle)) {
  38. echo "
  39. Output file name
  40. Output File Size
  41. Output File type
  42. Modified time of output file
  43. }
  44. Loop through the contents of the directory and know the last
  45. $dirFile = $dirname. " /". $file;
  46. Use the directory name and file name to link up so that you can use it in the filetype below
  47. if ($num ++%2==0) {
  48. Interlaced color by single complex
  49. $bgcolor = "#ffffff";
  50. }else{
  51. $bgcolor = "#cccccc";
  52. }
  53. echo "
  54. echo "
  55. echo "
  56. echo "
  57. echo "
  58. echo "
  59. "; "; "; "; "; "; "; "; "; ";

    Catalog ". $dirname." Under the content

    file name File Size File Type Modification Time
    ". $file."". FileSize ($dirFile)."". FileType ($dirFile)."". Filemtime ($dirFile)."
    ";
  60. Closedir ($dir _handle);
  61. echo " in Directory". $dirname. " ". $num." A file
    ";
  62. Disk_free_space ()
  63. Disk_total_space () Statistics disk size
  64. /*
  65. * Customize a recursive function to count the size of incoming directory files
  66. */
  67. function Dirsize ($directory) {
  68. $dir _size=0;
  69. Defines a variable that can be used to accumulate the size of each file to calculate the size of the directory
  70. if ($dir _handle=opendir ($directory)) {
  71. Open Directory
  72. while ($fileName =readdir ($dir _handle)) {
  73. Looping through files in a directory
  74. if ($fileName! = "." && $fileName! = "...") {
  75. Be sure to rule out two special directories.
  76. $subFile = $directory. " /". $fileName;
  77. Connect the file name and directory name
  78. if (Is_dir ($subFile)) {
  79. Determine if a sub-file is a directory
  80. $dir _size+=dirsize ($subFile);
  81. If it is a directory, continue to loop down
  82. }
  83. if (Is_file ($subFile)) {
  84. Determine if it is an ordinary file
  85. $dir _size+=filesize ($subFile);
  86. Gets the size of the file and adds up to the previous file size
  87. }
  88. }
  89. }
  90. }
  91. Closedir ($dir _handle);
  92. Closing a handle to a directory
  93. return $dir _size;
  94. }
  95. $dir _size=dirsize ("PM3");
  96. The size of the echo "directory PM3 is:". Round ($dir _size/pow (1024,2), 2). " MB ";
  97. The size of the output directory
  98. /*
  99. * Customize a recursive function to delete a directory
  100. */
  101. Unlink ()
  102. Delete the files in the directory
  103. function Deldir ($directory) {
  104. if (file_exists ($directory)) {
  105. Determine if a directory exists
  106. if ($dir _handle=opendir ($directory)) {
  107. Open Directory
  108. while ($fileName =readdir ($dir _handle)) {
  109. Iterate through the files in the directory
  110. if ($fileName! = "." && $fileName! = "...") {
  111. Be sure to exclude two special files, or you'll regret it.
  112. $subFile = $directory. " /". $fileName;
  113. Connect the file name and directory name.
  114. if (Is_dir ($subFile)) {
  115. If it is a directory, continue to execute itself
  116. Deldir ($subFile);
  117. }
  118. if (Is_file ($subFile)) {
  119. If it is a normal file, delete it directly
  120. Unlink ($subFile);
  121. }
  122. }
  123. }
  124. Closedir ($dir _handle);
  125. Close handle
  126. RmDir ($directory);
  127. Run to this is already empty directory, directly delete
  128. }
  129. }
  130. }
  131. Deldir ("PM4");
  132. /*
  133. * Customize a recursive function to copy or move a directory
  134. */
  135. Copy ()
  136. Copy an ordinary file
  137. mkdir ()
  138. Create a Directory
  139. function Copydir ($directory, $dirTo) {
  140. Two parameters, one is the source directory, the other is the target directory
  141. if (Is_file ($dirTo)) {
  142. Determine if the target is a normal file, exit the method directly
  143. echo "Target is not a directory, cannot complete replication";
  144. Return
  145. }
  146. if (!file_exists ($dirTo)) {
  147. Determine if the directory does not exist, create the directory
  148. mkdir ($dirTo);
  149. }
  150. if ($dir _handle=opendir ($directory)) {
  151. while ($fileName =readdir ($dir _handle)) {
  152. if ($fileName! = "." && $fileName! = "...") {
  153. $subFile = $directory. " /". $fileName;
  154. $subToFile = $dirTo. " /". $fileName;
  155. if (Is_dir ($subFile)) {
  156. Copydir ($subFile, $subToFile);
  157. }
  158. if (Is_file ($subFile)) {
  159. Copy ($subFile, $subToFile);
  160. }
  161. }
  162. }
  163. Closedir ($dir _handle);
  164. }
  165. }
  166. Copydir ("PM3", "pm4");
  167. ?>
Copy Code
Php
  • Related Article

    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.