Public documents
Sometimes, it is a pleasure to use PHP to list all files in a directory. To do this, use the directory class.
First, we must start to look at the directory of the directory object. Use 'in the current directory '. ', Because this references the current directory.
<? Php
$ Dir = dir (& apos;. & apos ;);
?>
The next part is a bit complex, but this is because of the PHP file recommendations, complicated, but short-term approach to do it.
I am using the simultaneous control structure through all entries in the loop. Under the conditions, I tested whether it is a directory-> "please read () and return false (right! = Carrier testing is of the correct type, so it is not '''rated as 0 or false because it will be non-type sensitive carrier ). I also confirm the expression in the $ file, but the transfer will return the final value of the file after the transfer. Dollar directory-> "please read () method will return false when there are no more files, it will break the loop.
<? Php
While (false! ==( $ File = $ dir-> read ())){
Echo $ file. "n ";
}
?>
If you are using an html file, you will use <br/> not a line break. Such a file will appear on the new production line on your webpage.
If you run the code before, you will find that the output of your script is listed in two strange files :. And... are all referring to the current directory (the '.'), and parent directory (in '..').
If you want to exclude these from your list, just write a declaration and ignore it if it checks whether the variable for the file is of these values if there is.
<? Php
While (false! ==( $ File = $ dir-> read ())){
If ($ file! = & Apos;. & apos; & $ file! = & Apos;... & apos ;){
Echo $ file. "n ";
}
}
?>
Therefore, it is necessary to close the instance directory level when you are working with them to clean the resources, and announce the operating system, you are done with it.
<? Php
$ Dir-> close ();
?>
This is the end of learning how to use PHP to list files. On the next page, you will learn how to upload files from a Directory Server. After I sent the next page to you, however, I actually used a directory in the archive list to retrieve external data from the Kirupa tutorial.
Yay, this is the last page! In the past, you learned more practical file processing skills, and we will summarize them on this page.