Jump to content

RecursiveIteratorIterator: exclude file


Go to solution Solved by requinix,

Recommended Posts

function main($dir, $md5ContainerFilename) {
    $elements = iterator_to_array(
            new RecursiveIteratorIterator(
                    new RecursiveDirectoryIterator($dir)
            )
    );

    $records = [];
    foreach ($elements as $element) {
        if (is_dir($element)){
            continue;
        }
        
        $record = md5($element) . ' ' . $element;

        array_push($records, $record);
    }

    $recordsStr = implode(PHP_EOL, $records);

    file_put_contents($md5ContainerFilename, $recordsStr);
}

main(
        '.',
        'dir/md5-container'
);
$ cat dir/md5-container 
b4d1a9a6ea915fbc185205f2d9b61caf ./dir/file
847971a473761ce82235c3f99d6a483b ./dir/file2
76efa814f29375feaf93f6b08982ae4e ./dir/md5-container
516f8127e52fadca9ede40e54a59f04b ./file
0a9990b3c55615b43bd0b0bbbf43dd06 ./file2

As you can see, among the various files, there is also the file dir/md5-container
This file should be the container, not part of the content.
How can I do to exclude it?
 

Link to comment
https://forums.phpfreaks.com/topic/314752-recursiveiteratoriterator-exclude-file/
Share on other sites

Get rid of that iterator_to_array. It's useless at best and wasteful at worst.

If you want to exclude something then you could write code to do so. Have you tried that yet? You could make use of one of the other existing iterators or simply look at $element directly.

function main($dir, $md5ContainerFilename) {
    $elements =
        new RecursiveIteratorIterator(
            new RecursiveDirectoryIterator($dir)
        )
    ;

    $records = [];
    foreach ($elements as $element) {
        if (is_dir($element)){
            continue;
        }
        if ($md5ContainerFilename == $element) {
            continue;
        }
        
        $record = md5($element) . ' ' . $element;

        array_push($records, $record);
    }

    $recordsStr = implode(PHP_EOL, $records);

    file_put_contents($md5ContainerFilename, $recordsStr);
}

main(
        '.',
        'dir/md5-container'
);
 
$ 
$ cat dir/md5-container
b4d1a9a6ea915fbc185205f2d9b61caf ./dir/file
847971a473761ce82235c3f99d6a483b ./dir/file2
76efa814f29375feaf93f6b08982ae4e ./dir/md5-container
516f8127e52fadca9ede40e54a59f04b ./file
0a9990b3c55615b43bd0b0bbbf43dd06 ./file2

$ grep md5 dir/md5-container 
76efa814f29375feaf93f6b08982ae4e ./dir/md5-container

 

Edited by block34
  • Solution

...and?

Take a look at your output: the MD5 hash on the left and the value of $element on the right. If you want to skip the md5-container then that would be when $element is...?
For a more useful solution, if what you care about is when the filename is "md5-container" and not what directory it's in then use a function like basename to get just the filename portion of $element.

But perhaps a smarter solution would be to not put your md5-container inside the directory you're trying to inspect. Don't have to skip over something if it's not there in the first place.

13 ore fa requinix ha detto:

...e?

Date un'occhiata alla vostra uscita: l'hash MD5, sulla sinistra, e il valore di $elemento sulla destra. Se si desidera saltare l'md5-contenitore, quindi, che quando $elemento è...?
Per una più proficua soluzione, se quello che ti interessa è quando il nome del file è "md5-container" e non quello che directory è quindi utilizzare una funzione come la base per ottenere solo il nome del file porzione di $elemento.

Ma forse una soluzione più intelligente sarebbe quello di non mettere il vostro md5-contenitore all'interno della directory in cui si sta cercando di controllare. Non devi saltare qualcosa se non c'è, in primo luogo.

ok, I will do so, thanks

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.