php Directory Traversal Function opendir Usage Example

  • 2021-08-03 09:49:35
  • OfStack

This article illustrates the usage of php directory traversal function opendir. Share it for your reference. The specific analysis is as follows:

The function opendir () opens the directory handle, returns 1 set of directory streams (1 set of directory strings) if the function runs successfully, and returns an error [error] if it fails. You can hide the error by adding "@" at the front of the function.

syntax Syntax: opendir (directory, context) parameter

Parameter: description

Description: directory required. specifies the directory to stream

Necessary parameters, specifying directory objects, optional parameters, specifying context of directory objects to be processed. This context includes a set of options, which can change the display mode of text stream. The example code is as follows:

<?php  
$dir = "./";
 
// open a known directory, and proceed to read its contents 
if (is_dir($dir)) 

if ($dh = opendir($dir)) { 
while (($file = readdir($dh)) !== false) { 
echo "filename: $file : filetype: " . filetype($dir . $file) . "n"."<br />"; 
}
closedir($dh); 


?>

I hope this article is helpful to everyone's PHP programming.


Related articles: