List directory files and subs-directories

Asked

Viewed 55 times

0

Good night,

I need to check files in directories and subdirectories. I am able to do, but I have to specify each directory, I would like to put only the root directory and then the code would do everything else. Example:

Root (file) Sub-Directory (archiv2) Sub-directory 2 (file 3)

Code node it would have to find the "file at the root", then enter subdirectory 1 and find the " file 1" and then find the "file 2" in subdirectory 2

who can help, thank you.

So far I have the code below :

<?php

    $dir = new DirectoryIterator( '/central/filial/');

    // array contendo os diretórios permitidos    
    $diretoriosPermitidos = array("loja2018","loja2019","loja2020");


    foreach($dir as $file)
    {
        // verifica se $file é diferente de '.' ou '..'
        if (!$file->isDot())
        {
            // listando somente os diretórios
            if  ( $file->isDir() )
            {
                // atribui o nome do diretório a variável
                $dirName = $file->getFilename();

                // listando somente o diretório permitido
                if( in_array($dirName, $diretoriosPermitidos)) {
                    // subdiretórios
                    $caminho = $file->getPathname();
                    // chamada da função de recursividade
                    recursivo($caminho, $dirName);
                }
            }

            // listando somente os arquivos do diretório
            if  ( $file->isFile() )
            {
                // atribui o nome do arquivo a variável
                $fileName = $file->getFilename();
                $filesize = $file->getSize();

                //
                echo "-----------------------------------------------------\n";
                    echo "Diretorio: Raiz"."\n"."Arquivo: ".$fileName."\n"."Tamanho: ".$filesize."\n";
                    echo "-----------------------------------------------------\n";
            }
        }
    }

    function recursivo( $caminho, $dirName ){

        global $dirName;

        $DI = new DirectoryIterator( $caminho );

        foreach ($DI as $file){
            if (!$file->isDot())
            {
                if  ( $file->isFile() && $file->getSize() < 500000 )
                {
                    //
                    $fileName = $file->getFilename();
                    $filesize = $file->getSize();
                    //
                    //echo "-----------------------------------------------------\n";
                    //echo "Diretorio: ".$dirName."\n"."Arquivo: ".$fileName."\n"."Tamanho: ".$filesize."\n";
                    //echo "-----------------------------------------------------\n";


                $conteudo = "";
                $conteudo .= "____________________________________________________________".PHP_EOL;
                $conteudo .= "Diretorio: $dirName".PHP_EOL;
                $conteudo .= "Arquivo: $fileName".PHP_EOL;
                $conteudo .= "Tamanho: $filesize".PHP_EOL;
                $conteudo .= "____________________________________________________________".PHP_EOL;
                $name = "/var/www/backup-ftpcre.php.txt";
                $file = fopen ($name, 'a+'); 
                fwrite($file, $conteudo);
                fclose($file);





                }
            }

        }
    }

    ?>

I managed to do it that way:

 <?php

$path = '/central/filial/';
$dir = new RecursiveDirectoryIterator ($path);
$iterator = new RecursiveIteratorIterator($dir);

foreach ($iterator as $item) {
    // Verifica se é um arquivo
    if ($item->isFile()) {
                $fileName = $item->getFilename();
                $filesize = $item->getSize();
                $path = $item->getPath();
                $date = $item->getMTime();


     if  ( $item->isFile() && $item->getSize() != 0 )
            {
                //echo "-----------------------------------------------------\n";
                //echo "Diretorio: ".$dirName."\n"."Arquivo: ".$fileName."\n"."Tamanho: ".$filesize."\n";
                //echo "-----------------------------------------------------\n";


            $conteudo = "";
            $conteudo .= "____________________________________________________________".PHP_EOL;
            $conteudo .= "Arquivo: $fileName".PHP_EOL;
            $conteudo .= "Tamanho: $filesize".PHP_EOL;
            $conteudo .= "Caminho: $path".PHP_EOL;
            $conteudo .= "Data de criacao: $date".PHP_EOL;
            $conteudo .= "____________________________________________________________".PHP_EOL;
            $name = "/var/www/backup-ftpcre.php.txt";
            $file = fopen ($name, 'a+'); 
            fwrite($file, $conteudo);
            fclose($file);

           }
        }
    }
?>

1 answer

0

Translating this answer:

You can use glob() with the option GLOB_ONLYDIR

or

$dirs = array_filter(glob('*'), 'is_dir');
print_r( $dirs);
  • could solve in parts, need to know also the file creation date or modification date. I am using the ->getMTime(); but it returns a value 1561819036 but would like it to return 29-06-2019.

  • Got it, converted it. $date = $item->getMTime();&#xA; $data = date ('d-m-Y', $date);

Browser other questions tagged

You are not signed in. Login or sign up in order to post.