block34 Posted May 2, 2022 Share Posted May 2, 2022 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? Quote Link to comment https://forums.phpfreaks.com/topic/314752-recursiveiteratoriterator-exclude-file/ Share on other sites More sharing options...
requinix Posted May 2, 2022 Share Posted May 2, 2022 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. Quote Link to comment https://forums.phpfreaks.com/topic/314752-recursiveiteratoriterator-exclude-file/#findComment-1595878 Share on other sites More sharing options...
block34 Posted May 2, 2022 Author Share Posted May 2, 2022 (edited) 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 May 2, 2022 by block34 Quote Link to comment https://forums.phpfreaks.com/topic/314752-recursiveiteratoriterator-exclude-file/#findComment-1595879 Share on other sites More sharing options...
Solution requinix Posted May 2, 2022 Solution Share Posted May 2, 2022 ...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. Quote Link to comment https://forums.phpfreaks.com/topic/314752-recursiveiteratoriterator-exclude-file/#findComment-1595881 Share on other sites More sharing options...
block34 Posted May 3, 2022 Author Share Posted May 3, 2022 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 Quote Link to comment https://forums.phpfreaks.com/topic/314752-recursiveiteratoriterator-exclude-file/#findComment-1595885 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.