Two examples of code for traversing directories in php
Release: 2016-07-25 08:53:49
Original
1077 people have browsed it
-
- /*
- * Traverse the directory
- * fopen()
- * fread()
- * fclose()
- * @opendir(target path);E_WARNINE
- * readdir(directory reference handle) rewinddir ()
- * closedir()
- *
- * In programming, a handle is a special smart pointer. When an application wants to reference memory blocks or objects managed by other systems (such as databases, operating systems)
- *, handles are used. The difference between a handle and an ordinary pointer is that
- * The pointer contains the memory address of the referenced object, while the handle is a reference identifier managed by the system.
- * This identifier can be relocated to a memory address by the system. This mode of indirect object access enhances the system's control over the referenced object.
- * (Script Academy bbs.it-home.org)
- */
- $dirHandle=@opendir("phpMyAdmin") or die("Unsuccessful in opening the directory");
- echo "All the contents in the phpMyAdmin directory are: < br>";
- echo readdir($dirHandle)."
";
- echo readdir($dirHandle)."
";
-
- while(($file=readdir($dirHandle))!= =false)
- {
- $file="phpMyadmin".DIRECTORY_SEPARATOR.$file;
- if(is_dir($file))
- {
- echo "Directory: ".$file."
";
- }else
- {
- echo "File: ".$file."File size: ".filesize($file)."KB
";
- }
- }
- rewinddir($dirHandle); //Return to the beginning of the handle and traverse it again
- while(($file=readdir($dirHandle))!==false)
- {
- $file="phpMyadmin".DIRECTORY_SEPARATOR.$file;
- if($file!="."&&$file!=". .") //Do not read directly
- {
- if(is_dir($file))
- {
- echo "Directory: ".$file."
";
- }else
- {
- echo "File: ". $file."File size: ".filesize($file)."KB
";
- }
- }
- }
- closedir($dirHandle);
- ?>
Copy code
Example 2,
-
- /*
- * Traverse directory
- *
- * class dir{
- * string path;
- * resource handle;
- * string read(void);
- * void rewind(void);
- * void close(void);
- * }
- *
- * In programming, a handle is a special smart pointer. When an application wants to reference memory blocks or objects managed by other systems (such as databases, operating systems)
- *, handles are used. The difference between a handle and an ordinary pointer is that
- * The pointer contains the memory address of the referenced object, while the handle is a reference identifier managed by the system.
- * This identifier can be relocated to a memory address by the system. This mode of indirect object access enhances the system's control over the referenced object.
- *
- */
- $d = dir("phpMyAdmin");
- echo "The path is: ".$d->path."
";
- echo "The reference handle is: ".$d- >handle."
";
- // $d->read();
- // $d->read();
- while(($file=$d->read() )!==false)
- {
- if($file!="."&&$file!="..")
- {
- echo $file."
";
- }
- }
- $d- >close();
- ?>
Copy code
|
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