PHP directory operations
Release: 2016-07-25 08:42:56
Original
1074 people have browsed it
-
- //basename()
- Return the file name part of the path
- $path="D:/lamp/apache2/htdocs/file.php";
- echo basename($path). "
";
- //Display the file name with the file extension
- echo basename($path,'php')."
";
- //Display the file name without the file extension
- //dirname()
- Remove the file name and return the directory name
- echo dirname($path)."
";
- //Return the directory name
- //pathinfo()
- Return an array of path attributes
- print_r(pathinfo($path))."
";
- //opendir()
- Open the specified directory
- //readdir()
- Read the specified directory
- //closedir()
- Close the specified directory
- //rewinddir()
- Rewind the directory handle
- /*
- *The following code is used to count the files in a directory
- */
- $num=0;
- //It is used to count the total number of subdirectories and files
- $dirname="pm3";
- //Define a directory, that is, the directory that needs to be traversed
- $dir_handle=opendir($dirname);
- //Open the directory
- //Output the traversed content and file names in table format
- echo "
";
- echo "
Directory".$dirname. "Content under";
- echo "
";
- echo "
File name | File sizeFile type | Modification time | ";
- while($file=readdir($ dir_handle)){
- //Loop through the contents of the directory until the end
- $dirFile=$dirname."/".$file;
- //Link the directory name and file name so that it can be used in the following filetype
- if($num++%2==0){
- //Realize interlaced colors through singular and plural numbers
- $bgcolor="#ffffff";
- }else{
- $bgcolor="#cccccc";
- }
- echo "< ;tr bgcolor='".$bgcolor."'>";
- echo "
".$file." | ";
- //Output file name
- echo "
" .filesize($dirFile)." | ";
- //Output file size
- echo "
".filetype($dirFile)." | ";
- //Output file type
- echo "
".filemtime($dirFile)." | ";
- //Output the modification time of the file
- echo "";
- }
- echo "
closedir($dir_handle); - echo "There are ".$num." files in the directory ".$dirname."
";
- //disk_free_space ()
- disk_total_space() Count disk size
- /*
- *Customize a recursive function to count the size of incoming directory files
- */
- function dirSize($directory){
- $dir_size=0;
- //Define an integer Variable, used to accumulate the size of each file to calculate the size of the directory
- if($dir_handle=opendir($directory)){
- //Open the directory
- while($fileName=readdir($dir_handle)){
- // Loop through the files in the directory
- if($fileName!="." && $fileName!=".."){
- //Be sure to exclude two special directories
- $subFile=$directory."/" .$fileName;
- //Connect the file name and directory name
- if(is_dir($subFile)){
- //Determine whether the sub-file is a directory
- $dir_size+=dirSize($subFile);
- //If it is a directory, continue Loop downward
- }
- if(is_file($subFile)){
- //Determine whether it is a normal file
- $dir_size+=filesize($subFile);
- //Get the size of the file and add it to the previous file size
- }
- }
- }
- }
- closedir($dir_handle);
- //The handle of the closed directory
- return $dir_size;
- }
- $dir_size=dirSize("pm3");
- echo "The size of directory pm3 is:".round ($dir_size/pow(1024,2),2)."MB";
- //The size of the output directory
- /*
- *Customize a recursive function to delete the directory
- */
- //unlink()
- Unlink()
- Delete the files in
- function delDir($directory){
- if(file_exists($directory)){
- //Determine whether the directory exists
- if($dir_handle=opendir($directory)){
- //Open the directory
- while ($fileName=readdir($dir_handle)){
- //Loop to read files in the directory
- if($fileName!="." && $fileName!=".."){
- //Be sure to exclude two Special file, otherwise you will regret it
- $subFile=$directory."/".$fileName;
- //Connect the file name and directory name
- if(is_dir($subFile)){
- //If it is a directory, continue The execution itself
- delDir($subFile);
- }
- if(is_file($subFile)){
- //If it is an ordinary file, delete it directly
- unlink($subFile);
- }
- }
- }
- closedir($dir_handle) ;
- //Close the handle
- rmdir($directory);
- //The directory you run here is already an empty directory, delete it directly
- }
- }🎜}
- //delDir("pm4");
- /*
- *Customize a recursive function to copy or move a directory
- */
- //copy()
- Copy an ordinary file
- //mkdir()
- Create a directory
- function copyDir($directory,$dirTo){
- //Two parameters, one is the source directory and the other is the target directory
- if(is_file($dirTo)){
- //Judge if the target is an ordinary file, then directly Exit method
- echo "The target is not a directory and the copy cannot be completed";
- return;
- }
- if(!file_exists($dirTo)){
- //Judge if the directory does not exist, then create the directory
- mkdir($dirTo) ;
- }
- if($dir_handle=opendir($directory)){
- while($fileName=readdir($dir_handle)){
- if($fileName!="." && $fileName!=".."){
- $subFile=$directory."/".$fileName;
- $subToFile=$dirTo."/".$fileName;
- if(is_dir($subFile)){
- copyDir($subFile,$subToFile);
- }
- if(is_file($subFile)){
- copy($subFile,$subToFile);
- }
- }
- }
- closedir($dir_handle);
- }
- }
- copyDir("pm3","pm4");
- ?>
-
Copy code
|
PHP
Statement of this Website
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
Latest Articles by Author
-
2024-10-22 09:46:29
-
2024-10-13 13:53:41
-
2024-10-12 12:15:51
-
2024-10-11 22:47:31
-
2024-10-11 19:36:51
-
2024-10-11 15:50:41
-
2024-10-11 15:07:41
-
2024-10-11 14:21:21
-
2024-10-11 12:59:11
-
2024-10-11 12:17:31