简体   繁体   中英

php : scan dir and save into txt with filter option

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);
}
}

glob() could be what you're looking for:
https://www.php.net/manual/en/function.glob.php

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM