Php dir function tutorial

Source: Internet
Author: User

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.

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.