Skip to content

Instantly share code, notes, and snippets.

@fedmich
Last active May 8, 2023 00:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fedmich/8ec847b559675914a4a91bb335697be3 to your computer and use it in GitHub Desktop.
Save fedmich/8ec847b559675914a4a91bb335697be3 to your computer and use it in GitHub Desktop.
<?php
$fol = '/var/www/path';
$files = preg_ls2( $fol, false, '@.*\.txt@i' );
var_dump( $files );
?>
<?php
function preg_ls2 ($path=".", $rec=false, $pat="/.*/") {
$pat=preg_replace("|(/.*/[^S]*)|s", "\\1S", $pat);
while (substr($path,-1,1)=="/") $path=substr($path,0,-1);
if (!is_dir($path)) $path=dirname($path);
if ($rec!==true) $rec=false;
$d=dir($path);
$ret=Array();
while (false!==($e=$d->read())) {
if (($e==".")||($e=="..")) continue;
if ($rec && is_dir($path."/".$e)) {
$ret=array_merge($ret,preg_ls($path."/".$e,$rec,$pat));
continue;
}
if (!preg_match($pat,$e)) continue;
$ret[]=$e;
}
$d->close();
return $ret;
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment