Php creates directory folder function mkdir (). Its structure is as follows:
Kdir (string $ dirname, [int $ mode])
The $ dirname parameter is the name of the directory to be created. The $ mode parameter is optional. It is an integer variable and indicates the creation mode.
Instance:
The code is as follows: |
Copy code |
<? Php $ Name = "php "; $ D = mkdir ($ name, 0777);/* 0777 indicates the maximum access permission */ If ($ d ){ Echo "created successfully "; } Else echo "creation failed "; ?> |
Php provides many methods to traverse directories. Here, we will introduce in detail how php traverses files in directories through examples.
Code:
The code is as follows: |
Copy code |
<? Php Function directory ($ dir) {/* declare a function */ $ Dp = opendir ($ dir);/* open the directory */ While ($ file = readdir ($ dp) {/* read directory */ If ($ file! = "." & $ File! = "..") {/* Determine whether there is a ".." or ".." file */ $ Path = $ dir. "/". $ file;/* obtain the directory path */ If (is_dir ($ path) {/* determine whether a subdirectory exists */ Directory ($ path);/* recursive function call */ } Else echo $ path. "<br>";/* Display file */ } } Closedir ($ dp ); } Directory ("e: wp "); ?> |
The php directory reading function readdir () can read all files and folders in the directory. Its structure is as follows:
Readdir ($ dp );
The $ dp parameter is used to open the resource object returned by the Directory using the opendir () function. The function returns the file name under the Directory.
Instance:
The code is as follows: |
Copy code |
<? Php $ Dir = opendir ("study "); While ($ read = readdir ($ dir )){ Print ($ read. "<br> "); } ?> |
Php uses closedir () to close the directory function. Its structure is as follows:
Closedir ($ dp)
The $ dp parameter is used to open the resource object returned by the Directory using the opendir () function.
Instance:
The code is as follows: |
Copy code |
<? Php $ Mulu = "study "; $ Dir = opendir ($ mulu ); Closedir ($ dir ); ?>
|
If the function closedir () is used to close the directory successfully, the value 1 is not returned. Therefore, you cannot use the if statement to determine whether the directory is successfully closed.
I have introduced a series of php file operations, and then I will introduce how to operate directories. Php Directory functions are similar to File functions. Here we first introduce the Open Directory function opendir (). Its structure is as follows:
Opendir (string $ path)
The $ path parameter is the path of the directory to be opened. The function returns a handle to open the directory to store the current directory resources. Before opening the directory, you must first determine whether the directory exists and use the is_dir () function.
Instance:
The code is as follows: |
Copy code |
<? Php If (is_dir ("stufdy ")){ Opendir ("studfy "); Print_r ("directory opened successfully "); } Else Echo "the directory does not exist "; ?> |
The php pointer function rewind () can set the file position pointer to the beginning of the file. Its structure is as follows:
Bool rewind (resource $ handle );
The function returns a boolean value. If the operation succeeds, true is returned. If the operation fails, false is returned.
Instance:
The code is as follows: |
Copy code |
<? Php $ F = fopen ("php.txt", "r "); Echo fgets ($ f). "<br>";/* output the first line */ Echo fgets ($ f). "<br>";/* output the second line */ Rewind ($ f);/* pointer returns the file header */ Echo fgets ($ f);/* output the first line */ ?>
|