Display content of the directory | PHP - IProgramX

Q. Write program to read directory name from user and display content of the directory.  


HTML File:


<html>
<form action="a3a2.php" method="POST">
<h1>Display directory contents</h1>
<br>
<h3>Enter directory name : <input type="text" name='d'></h3>
<pre>
                <input type="submit" name="submit" value="submit">

</pre>
</form>


</body>
</html>

PHP Function:
NOTE: PHP function is saved as "a3a2.php"


<?php

$d=$_POST['d'];
$h=dir(".");
if(is_dir($d))
{
        $h=opendir($d);
        echo "<h3><B>Directory Contents";
        echo "<br/>";

        while(($file=readdir($h))!==false)

        {
                echo "<h3>$file<br/>";
        }

        closedir($h);
}
else
{
        echo "<center><h2><center><u>Invalid Directory or Directory does not exist!!!</u></center></h2></center>";
}
?>

Output:



Post a Comment

1 Comments