1. Open/close file directory
Opening/Closing the file directory is similar to opening/closing files, but if the open file does not exist, a file is created automatically. If the path to the open file is incorrect, an error will be taken.
(1) Open Directory
PHP uses the Opendir () function to open the directory with the following syntax:
Resource Opendir (string path)
Path is a valid directory path and returns a directory pointer after successful execution. Returns False if path is not a valid directory path or cannot be opened because of a permission or system error. and generates an error warning. The output of the error message can be suppressed by adding an ' @ ' character to the front of the Opendir ().
(2) Close the directory
Close the directory using the Closedir () function with the following syntax:
void Closedir (Resource handle)
The parameter handle is a directory pointer returned by the OPENDIR () function.
The sample code for the open/close directory is as follows:
<?php$filepath='/applications/xampp/xamppfiles/htdocs/phptest/';if(Is_dir ($filepath)) {//Detect if it is a directory if($dir=opendir ($filepath)) {Echo "The directory is as follows:";Echo $dir; }Else{Echo "<<<< Directory Error";Exit(); } closedir ($dir);}Else{Echo "Directory Error >>>";}?>
The results of the operation are as follows:
(3) Browse the catalogue
The browse directory in PHP uses the Scandir () function, with the following syntax:
Array Scandir (string Diretory[,int Sorting_order])
The function returns all the files and directories under the directory, the parameter sorting_order specifies the sort order, and the default alphabetical order. If parameters are added, they are sorted in descending order.
The sample code is as follows:
<?php $filepath = '/applications/xampp/xamppfiles/htdocs/phptest/' ; if (Is_dir ( $filepath )) {//detect if it is a directory $dir =scandir ( $filepath ); foreach ( $dir as $value ) {echo $value . ' <br> ' ; }}else {echo "directory error >>> ";} ?>
The results of the operation are as follows:
The following table is a common directory manipulation function:
PHP Development File Directory processing