Some concepts should be seen.
Recursive and non-rerecursive iterators
PHP has no iterators
"recursive", as in ArrayIterator
and FilesystemIterator
. There are also
"recursive" iterators, such as RecursiveArrayIteratore
RecursiveDirectoryIterator
. The latter have methods that allow them
deepen, the first not.
When the instances of these iterators are executed in loop by
own account, even recursives, values only come from the level
"top" even if they are looped in an array or directory
nestled with subdirectories.
Source: https://stackoverflow.com/a/12235779/11379709
$diretorio = '.'; //Caminho do diretório a ser vasculhado.
//Obtém um iterador não recursivo que lista caminho dos arquivos do diretório
//assim como os arquivos de seus subdiretórios.
$IteratorRecursivo = new RecursiveIteratorIterator(
new RecursiveDirectoryIterator($diretorio)
);
//Filtra os arquivos com extensão .jpg e cria um array com o caminho desses arquivos.
$Arquivos = array_keys(
iterator_to_array(
new RegexIterator(
$IteratorRecursivo, '/^.+\.jpg$/i',
RegexIterator::GET_MATCH
)
)
);
//Exclui os arquivos .jpg do diretórios e de seus subdiretórios.
array_map("unlink", $Arquivos);
I can do it effortlessly with the class Recursivedirectoryiterator. But I don’t know if using an iterator serves you.
– Augusto Vasques
On my website the user about a . zip, I do the unzipping, but I know it is possible to have inside the zip malicious files, or files that should not be there, With the array_map('unlink', glob($directory."*.jpg")); I hit jpg files only from the root folder, But I fear that users go up in zip sub folders and inside them imprudent files so to speak> I would need a way to go through all possible subfolders and go deleting files. jpg for example .
– Claudinho Rainho
I found ways to scan the main folder, but I can’t figure out a way to access subfolders I don’t know the names of
– Claudinho Rainho
It is also possible to delete directly in the zipped file, but I believe it is the case to ask another question.
– Augusto Vasques