Open Directory in PHP – opendir() Function
The opendir() function is used to open files from directory.
opendir() function syntax
<?php
$dir = opendir(“directory_name”);
?>
opendir() function Example
<?php
$dir=opendir("meera");
?>
Here, we use opendir() function to open directory “meera” and store in $dir variable, we use the $dir variable for read content of file in future.
Read files from directory using readdir() function and opendir() function.
Exmaple of readdir() in PHP
<HTML>
<head>
<title>Read Diretory in PHP</title>
</head>
<body>
<FORM method="POST">
Enter Directory Name : <input type="text" name="str"> <br/> <br/>
<input type="submit" name="Submit1" value="Read Files">
</FORM>
<?php
if(isset($_POST["Submit1"]))
{
$dir=opendir($_POST["str"]);
while($file=readdir($dir))
{
echo $file ."<br/>";
}
}
?>
</body>
</HTML>
The output is :