search
HomeBackend DevelopmentPHP TutorialPHP directory operations

  1. //basename()
  2. Return the file name part of the path
  3. $path="D:/lamp/apache2/htdocs/file.php";
  4. echo basename($path). "
    ";
  5. //Display the file name with the file extension
  6. echo basename($path,'php')."
    ";
  7. //Display the file name without the file extension
  8. //dirname()
  9. Remove the file name and return the directory name
  10. echo dirname($path)."
    ";
  11. //Return the directory name
  12. //pathinfo()
  13. Return an array of path attributes
  14. print_r(pathinfo($path))."
    ";
  15. //opendir()
  16. Open the specified directory
  17. //readdir()
  18. Read the specified directory
  19. //closedir()
  20. Close the specified directory
  21. //rewinddir()
  22. Rewind the directory handle
  23. /*
  24. *The following code is used to count the files in a directory
  25. */
  26. $num=0;
  27. //It is used to count the total number of subdirectories and files
  28. $dirname="pm3";
  29. //Define a directory, that is, the directory that needs to be traversed
  30. $dir_handle=opendir($dirname);
  31. //Open the directory
  32. //Output the traversed content and file names in table format
  33. echo "";
  34. echo "
  35. ";
  36. echo "
  37. ";
  38. echo "
  39. > ;";
  40. while($file=readdir($ dir_handle)){
  41. //Loop through the contents of the directory until the end
  42. $dirFile=$dirname."/".$file;
  43. //Link the directory name and file name so that it can be used in the following filetype
  44. if($num++%2==0){
  45. //Realize interlaced colors through singular and plural numbers
  46. $bgcolor="#ffffff";
  47. }else{
  48. $bgcolor="#cccccc";
  49. }
  50. echo "< ;tr bgcolor='".$bgcolor."'>";
  51. echo "
  52. ";
  53. //Output file name
  54. echo "
  55. ";
  56. //Output file size
  57. echo "
  58. ";
  59. //Output file type
  60. echo "
  61. ";
  62. //Output the modification time of the file
  63. echo "";
  64. }
  65. echo "
  66. Directory".$dirname. "Content under

    File nameFile size File type Modification time
    ".$file." " .filesize($dirFile)." ".filetype($dirFile)." ".filemtime($dirFile)."
    > ;";
  67. closedir($dir_handle);
  68. echo "There are ".$num." files in the directory ".$dirname."
    ";
  69. //disk_free_space ()
  70. disk_total_space() Count disk size
  71. /*
  72. *Customize a recursive function to count the size of incoming directory files
  73. */
  74. function dirSize($directory){
  75. $dir_size=0;
  76. //Define an integer Variable, used to accumulate the size of each file to calculate the size of the directory
  77. if($dir_handle=opendir($directory)){
  78. //Open the directory
  79. while($fileName=readdir($dir_handle)){
  80. // Loop through the files in the directory
  81. if($fileName!="." && $fileName!=".."){
  82. //Be sure to exclude two special directories
  83. $subFile=$directory."/" .$fileName;
  84. //Connect the file name and directory name
  85. if(is_dir($subFile)){
  86. //Determine whether the sub-file is a directory
  87. $dir_size+=dirSize($subFile);
  88. //If it is a directory, continue Loop downward
  89. }
  90. if(is_file($subFile)){
  91. //Determine whether it is a normal file
  92. $dir_size+=filesize($subFile);
  93. //Get the size of the file and add it to the previous file size
  94. }
  95. }
  96. }
  97. }
  98. closedir($dir_handle);
  99. //The handle of the closed directory
  100. return $dir_size;
  101. }
  102. $dir_size=dirSize("pm3");
  103. echo "The size of directory pm3 is:".round ($dir_size/pow(1024,2),2)."MB";
  104. //The size of the output directory
  105. /*
  106. *Customize a recursive function to delete the directory
  107. */
  108. //unlink()
  109. Unlink()
  110. Delete the files in
  111. function delDir($directory){
  112. if(file_exists($directory)){
  113. //Determine whether the directory exists
  114. if($dir_handle=opendir($directory)){
  115. //Open the directory
  116. while ($fileName=readdir($dir_handle)){
  117. //Loop to read files in the directory
  118. if($fileName!="." && $fileName!=".."){
  119. //Be sure to exclude two Special file, otherwise you will regret it
  120. $subFile=$directory."/".$fileName;
  121. //Connect the file name and directory name
  122. if(is_dir($subFile)){
  123. //If it is a directory, continue The execution itself
  124. delDir($subFile);
  125. }
  126. if(is_file($subFile)){
  127. //If it is an ordinary file, delete it directly
  128. unlink($subFile);
  129. }
  130. }
  131. }
  132. closedir($dir_handle) ;
  133. //Close the handle
  134. rmdir($directory);
  135. //The directory you run here is already an empty directory, delete it directly
  136. }
  137. }🎜}
  138. //delDir("pm4");
  139. /*
  140. *Customize a recursive function to copy or move a directory
  141. */
  142. //copy()
  143. Copy an ordinary file
  144. //mkdir()
  145. Create a directory
  146. function copyDir($directory,$dirTo){
  147. //Two parameters, one is the source directory and the other is the target directory
  148. if(is_file($dirTo)){
  149. //Judge if the target is an ordinary file, then directly Exit method
  150. echo "The target is not a directory and the copy cannot be completed";
  151. return;
  152. }
  153. if(!file_exists($dirTo)){
  154. //Judge if the directory does not exist, then create the directory
  155. mkdir($dirTo) ;
  156. }
  157. if($dir_handle=opendir($directory)){
  158. while($fileName=readdir($dir_handle)){
  159. if($fileName!="." && $fileName!=".."){
  160. $subFile=$directory."/".$fileName;
  161. $subToFile=$dirTo."/".$fileName;
  162. if(is_dir($subFile)){
  163. copyDir($subFile,$subToFile);
  164. }
  165. if(is_file($subFile)){
  166. copy($subFile,$subToFile);
  167. }
  168. }
  169. }
  170. closedir($dir_handle);
  171. }
  172. }
  173. copyDir("pm3","pm4");
  174. ?>
Copy code

PHP


Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
The Continued Use of PHP: Reasons for Its EnduranceThe Continued Use of PHP: Reasons for Its EnduranceApr 19, 2025 am 12:23 AM

What’s still popular is the ease of use, flexibility and a strong ecosystem. 1) Ease of use and simple syntax make it the first choice for beginners. 2) Closely integrated with web development, excellent interaction with HTTP requests and database. 3) The huge ecosystem provides a wealth of tools and libraries. 4) Active community and open source nature adapts them to new needs and technology trends.

PHP and Python: Exploring Their Similarities and DifferencesPHP and Python: Exploring Their Similarities and DifferencesApr 19, 2025 am 12:21 AM

PHP and Python are both high-level programming languages ​​that are widely used in web development, data processing and automation tasks. 1.PHP is often used to build dynamic websites and content management systems, while Python is often used to build web frameworks and data science. 2.PHP uses echo to output content, Python uses print. 3. Both support object-oriented programming, but the syntax and keywords are different. 4. PHP supports weak type conversion, while Python is more stringent. 5. PHP performance optimization includes using OPcache and asynchronous programming, while Python uses cProfile and asynchronous programming.

PHP and Python: Different Paradigms ExplainedPHP and Python: Different Paradigms ExplainedApr 18, 2025 am 12:26 AM

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP and Python: A Deep Dive into Their HistoryPHP and Python: A Deep Dive into Their HistoryApr 18, 2025 am 12:25 AM

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

Choosing Between PHP and Python: A GuideChoosing Between PHP and Python: A GuideApr 18, 2025 am 12:24 AM

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

PHP and Frameworks: Modernizing the LanguagePHP and Frameworks: Modernizing the LanguageApr 18, 2025 am 12:14 AM

PHP remains important in the modernization process because it supports a large number of websites and applications and adapts to development needs through frameworks. 1.PHP7 improves performance and introduces new features. 2. Modern frameworks such as Laravel, Symfony and CodeIgniter simplify development and improve code quality. 3. Performance optimization and best practices further improve application efficiency.

PHP's Impact: Web Development and BeyondPHP's Impact: Web Development and BeyondApr 18, 2025 am 12:10 AM

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

How does PHP type hinting work, including scalar types, return types, union types, and nullable types?How does PHP type hinting work, including scalar types, return types, union types, and nullable types?Apr 17, 2025 am 12:25 AM

PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values ​​and handle functions that may return null values.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

Atom editor mac version download

Atom editor mac version download

The most popular open source editor