PHP Directory Processing

Source: Internet
Author: User

The directory is also a file and is a special kind of file. To browse the file under the directory, you must first open the directory, and then close the directory once you have finished browsing. In this regard, there is no difference between the two, it takes 3 steps to open the directory, browse the directory, and close the directory.
Open DirectoryOpening a directory is similar to opening a file, but if the open file does not exist, a new file is created automatically, and if the Open Directory is incorrect, an error will be made. Use the Opendir () function in PHP to open the directory. The syntax is as follows: Resource Opendir (string path) parameter path is a valid directory zero, returns a pointer to the directory after successful execution, if path does not have a valid directory, or because the permissions, file system errors cannot open the directory, Opendir () The function returns false and produces an e_warning level error message. You can suppress the output of the error message by adding an "@" symbol to the front of the Opendir ().

Browse CatalogsThe files in the directory that are browsed in PHP use the Scandir () function. The syntax is as follows: Array Scandir (String Directory[,int sorting_order]) The function returns an array that contains all the files and directories in the directory. The parameter sorting_order specifies the sort order, which is sorted alphabetically by default, and becomes descending if the parameter sorting_order is applied. Example: Browse all files under the root directory


$path = ". /test ", if (Is_dir ($path)) {$dir =scandir ($path), foreach ($dir as $value) {echo $value. <br> ";    }} Else{echo "directory path error";}

The result of the operation is:.
..
. idea
15-test
CSDN
Count.txt
index1.php
Num
phpyudingyichangliang.php
Test.js
test.php
Test.txt
Upfile


Operating DirectoryBecause the directory is a special file, the operation of the file processing functions (such as renaming, etc.) are generally tried and directories. PHP also provides some special operating functions for the directory itself. The following table lists some common directory manipulation functions:
function prototype function description example
bool mkdir (String pathname) Create a new specified directory mkdir (" temp ")
bool rmdir (String dirname) Delete the specified directory, the directory must be empty rmdir (" temp ")
string getcwd (void) Get the current working directory GETCWD ()
bool chdir (String directory) Change the current directory to diretory echo getcwd (). ' <br> '; ChDir ('. /'); echo getcwd (). ' <br> ';
float disk_free_space (String directory) Returns the available space in the directory (bytes). The files being inspected must pass through the server's file system. disk_free_space (' D:\\wampserver ');
float disk_total_space (String directory) Returns the total space size of the directory (bytes) disk_total_space (' D:\\wampserver ');
string readdir (Resource handle) Returns the file name of the next file in the directory (when using this function, the directory must be opened using the Opendir () function) before PHP5, using this function to navigate the directory's while (false!== ($path ==readdir ($handle))) {    echo $pah;}
void Rewinddir (Resource handle) Re-refers the specified directory to the beginning of the directory Rewinddir ($handle)



Close DirectoryThe directory is closed in PHP via the Closedir () function. The syntax is as follows void Closedir (resource handle) parameter handle the directory pointer returned for a directory opened with the Opendir () function For example, the following is a process code for opening and closing a directory:
$path = ". /"; if (Is_dir ($path)) {if ($dire =opendir ($path)) echo $dire;} Else{echo "Path error"; exit ();} //....  Other Operation Closedir ($dire);

The result of the operation is:
Resource ID #2
Note: the Is_dir () function is used to determine whether the current path is a valid directory. Returns true if valid, otherwise false is returned.





PHP Directory Processing

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.