Skip to content

Instantly share code, notes, and snippets.

@andrei-coelho
Last active April 6, 2021 01:55
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 andrei-coelho/3ef7d7f6482d402c09dc935f85b4befb to your computer and use it in GitHub Desktop.
Save andrei-coelho/3ef7d7f6482d402c09dc935f85b4befb to your computer and use it in GitHub Desktop.
Get the list with the name of all files in a given directory / Recupera a lista com o nome de todos os arquivos de um determinado diretório
<?php
/**
* scan_dir
*
* Esta função faz uso de recursividade
* para recuperar um lista de arquivos
* em um determinado diretório, incluindo
* os arquivos de diretórios internos
*
*/
scan_dir("path/do/dir/");
function scan_dir($dir, $res = [], $prefix = false){
$scan = scandir($dir);
foreach ($scan as $key => $value){
if (!in_array($value, [".",".."])){
$d = $dir . DIRECTORY_SEPARATOR . $value;
$v = $prefix ? $prefix."/".$value : $value;
if (is_dir($d)){
$res = scan_dir($d, $res, $v);
continue;
}
$res[] = $v;
}
}
return $res;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment