Phpdir function tutorial _ PHP Tutorial

Source: Internet
Author: User
Phpdir function tutorial. Listing files sometimes makes it 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. 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.

$ Dir = dir ('.');
?>


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.


While (false! ==( $ File = $ dir-> read ())){
Echo $ file. "n ";
}
?>


If you are using an html file, you will use
It is not a line break, and 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.

While (false! ==( $ File = $ dir-> read ())){
If ($ file! = '.' & $ File! = '..'){
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.

$ 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.

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 ....

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.