0
I have a script, which basically compares the files of a bank table with the files of a directory, and it shows the files that are NOT in the database and are in the directory.
I have 2 problems:
- Count the amount of files that nay are in the bank and are in the directory
- Display message with this amount, only once inside the
foreach
Code I am using:
<?php
include('menu.php'); include('connect.php');
$files = glob("teste-uploads/*.{pdf,jpg,png}", GLOB_BRACE); // Encontra nomes dos arquivos de determinada pasta e salva no array $files
$Busca_Imagens = mysqli_query($con,"SELECT fotos FROM teste"); // Busca nome das imagens no seu Banco de Dados
while($Checa_Imagens = mysqli_fetch_assoc($Busca_Imagens)){
$images[] = 'teste-uploads/' . $Checa_Imagens['fotos'];
};
foreach($files as $file){
if(!in_array($file, $images)){
echo "Cerca de <b>" .count($file). "</b> arquivo(s) que não estão no banco de dados <p></p>"; // Número total
echo "<b>" . $file . "</b><br>"; // Exibe os arquivos
}}?>
The problem is that it is not counting correctly and the phrase "About ..." is repeating itself to each file.
It worked, thank you very much!
– Douglas
Tiago, can you tell me if I can mention all the subfolders of a folder at once? in part
$files = glob("teste-uploads/*.{pdf,jpg,png}", GLOB_BRACE);
– Douglas
There is:
$iterator = new RecursiveTreeIterator(new RecursiveDirectoryIterator("teste-uploads/", RecursiveDirectoryIterator::SKIP_DOTS));
foreach($iterator as $path) {
 echo $path."<br>";
}
– TiagoA
I didn’t quite understand the huahuaha code, I will test it here. I have a folder called
uploads
, in it, has 12 sub-folders, with the months of the year... I can do what I mentioned in the post by selecting all the subfolders at once, so?– Douglas
Yes, in case you want to filter can still improve with the
RecursiveRegexIterator
:$rd = new RecursiveDirectoryIterator("teste-uploads/", RecursiveDirectoryIterator::SKIP_DOTS);
$rr = new RecursiveRegexIterator($rd, '/\.(pdf|jpg|png)$/');
$it = new RecursiveTreeIterator($rr);
foreach($it as $path) {
 echo $path."<br>";
}
– TiagoA
Of course, just make sure the shape of the vailable
$path
is the same as in the bank.– TiagoA
But, how would I reference the fields? in that part of the code:
{
 $images[] = 'teste-uploads/' . $Checa_Imagens['fotos'];
}
, are 12 fields in the table, the 12 have document path...– Douglas