This article summarizes the PHP directory operation of some functions and use of methods including: Create directory, traverse directory, read directory, close directory, open directory and so on PHP directory operation function, there is a need for friends to refer to.
PHP creates the Directory folder function mkdir (), which is structured as follows:
Kdir (string $dirname, [int $mode])
The parameter $dirname is the name of the directory you want to create, and the parameter $mode is optional, as an integer variable that represents the creation pattern.
Instance:
The code is as follows |
Copy Code |
$name = "PHP"; $d =mkdir ($name, 0777); /* 0777 indicates maximum access rights */ if ($d) { echo "created successfully"; } else echo "Creation unsuccessful"; ?> |
PHP is a lot of ways to traverse the directory, here is a detailed example of PHP traversal directory file method.
Code:
The code is as follows |
Copy Code |
function directory ($DIR) {/* declaring functions */ $DP =opendir ($dir); /* Open Directory */ while ($file =readdir ($DP)) {/* Read directory */ if ($file! = "." && $file! = "...") {/* Determine if there is a "." or ".." FILE */ $path = $dir. "/". $file; /* Get directory path */ if (Is_dir ($path)) {/* Determine if there are subdirectories */ Directory ($path); /* Function Recursive call */ } else echo $path. " "; /* Display file */ } } Closedir ($DP); }
Directory ("E:WP"); ?> |
PHP Read directory function Readdir () can read all the files and folders in this directory, which is structured as follows:
Readdir ($DP);
The parameter $DP the resource object returned by using the function Opendir () to open the directory, and the function returns the file name under the directory.
Instance:
The code is as follows |
Copy Code |
$dir =opendir ("study"); while ($read =readdir ($dir)) { Print ($read. " "); } ?> |
PHP closes the directory function with Closedir (), which is structured as follows:
Closedir ($DP)
The parameter $DP the resource object returned by using the function Opendir () to open the directory.
Instance:
The code is as follows |
Copy Code |
$mulu = "study"; $dir =opendir ($mulu); Closedir ($dir); ?>
|
Using the function Closedir () to close a directory successfully does not return a value of 1, so the IF statement cannot be used to determine whether the closure succeeds
This article introduces a series of PHP file operations, and then the author introduces how to operate the directory. PHP directory function function is similar to the file function, here first introduced the Open Directory function Opendir (), the structure of the form as follows:
Opendir (String $path)
The parameter $path is the path to open the directory, and the function returns a handle to the Open directory to store the current directory resource. Before opening the directory, you must first determine if the directory exists, using the Is_dir () function.
Instance:
The code is as follows |
Copy Code |
if (Is_dir ("Stufdy")) { Opendir ("Studfy"); Print_r ("Directory opened successfully"); } Else echo "Directory does not exist"; ?> |
The PHP pointer function rewind () sets the file position pointer to the beginning of the file, which is structured as follows:
BOOL Rewind (resource $handle);
The function returns a Boolean value that returns True if successful, and false if it fails.
Instance:
The code is as follows |
Copy Code |
$f =fopen ("Php.txt", "R"); Echo fgets ($f). " "; /* Output First line */ Echo fgets ($f). " "; /* Output Second line */ Rewind ($f); /* Pointer returns the file header */ Echo fgets ($f); /* Output First line */ ?>
|
http://www.bkjia.com/PHPjc/628848.html www.bkjia.com true http://www.bkjia.com/PHPjc/628848.html techarticle This article summarizes the PHP directory operation of some functions and use of methods including: Create directory, traverse directory, read directory, close directory, open directory and so on PHP directory operation letter ...