Q. Write a program to read directory name and extension. Display the files with specified extension from that directory.
HTML File:
PHP Function:NOTE: PHP function is saved as "ass3b2.php"
<?php
$d=$_POST['d'];
$ext=$_POST['ex'];
if(is_dir($d))
{
$h=opendir($d);
echo "<h3>The files with extension $ext in directory $d are: </h3>";
while(($file=readdir($h))!==false)
if(is_dir($file))
{
continue;
}
$e=explode('.',$file);
if($e[1]==$ext)
{
echo "<h3>$file</h3><br/>";
}
}
closedir($h);
}
?>
Output:
HTML File:
<html>
<head><title>Exercise 3 Set A 1</title></head>
<body bgcolor="grey">
<form action="a3b2.php" method="POST">
<center><h1><b><font type="San Sarif">Search file of given extension.</font></b></h1></center>
<br>
<br>
<hr>
<h3>Enter directory name: <input type="text" name="d"></h3>
<h3>Enter file extension: <input type="text" name="ex"></h3>
<pre>
<input type="submit" name="submit" value="submit">
<input type="reset" name="reset'" value="reset">
</pre>
</form>
</body>
</html>
PHP Function:NOTE: PHP function is saved as "ass3b2.php"
<?php
$d=$_POST['d'];
$ext=$_POST['ex'];
if(is_dir($d))
{
$h=opendir($d);
echo "<h3>The files with extension $ext in directory $d are: </h3>";
while(($file=readdir($h))!==false)
if(is_dir($file))
{
continue;
}
$e=explode('.',$file);
if($e[1]==$ext)
{
echo "<h3>$file</h3><br/>";
}
}
closedir($h);
}
?>
Output:
0 Comments