php : scan dir and save into txt with filter option

Viewed 17

I have php funcation that scan dir and save content list into text.It works well but now i want to add filter funcation that restrict given extentoin to prevent to save into text.

function dirscan($dir,$file){
$folder = scandir($dir);
natsort($folder);
foreach ($folder as $value)
{
{
if ($value != '.' && $value != '..')
$val=$value."\r\n";
}
$fh = fopen($file,'a');
fwrite($fh,$val);
}
}
2 Answers
function dirscan($dir,$file,$ext){
$folder = scandir($dir);
natsort($folder);
foreach ($folder as $value)
{
{
if ($value != '.' && $value != '..'&& pathinfo($value, PATHINFO_EXTENSION) !== $ext)
$val=$value."\r\n";
}
$fh = fopen($file,'a');
fwrite($fh,$val);
}
}

$file_temp='list.txt';
unlink($file_temp);

credit for hint :Lawrence Cherone

Related