How to avoid repeating the same output with foreach nested loops. Or how to implement the code so that this does not happen? Example code where nested loops crawl files:

$spisok_files = ftp_nlist($antona_connect, $file_remote); foreach ($spisok_files as $key => $value_rem) { $ftp_size_fil = ftp_size($antona_connect, $file_remote . $value_rem); echo "MD5 хэш размера ftp файла " . $value_rem . "- " . $md5_rem = md5($ftp_size_fil) . "<br>"; $local_scan = scandir($file_local); foreach ($local_scan as $key_scan => $value_loc) { if (!is_dir($value_loc)) { echo "Сравнивают удаленный $value_rem с локальным $value_loc<br>"; $size_local = filesize($file_local . $value_loc); echo "MD5 хэш размера локального файла " . $value_loc . "- " . $md5_loc = md5($size_local) . "<br>"; if ($md5_loc !== $md5_rem AND $value_rem == $value_loc) { echo "Загружаю++++++++++++++++++++++++++<br>"; ftp_nod_upload($value_loc); } else { echo "Забиваю-------------------------------<br>"; } } } } 

    1 answer 1

    I understand that the purpose of the script is to verify files on the server and locale. THEN I would do differently - create an array of files on the locale with its MD5, that is, as a result, a two-dimensional array (the data on the file is also in the array), then I would create the same array of data on local files.

    Next, we check, and if we find matches of the files - you don’t need to upload them, then remove this file from the local list (how would it be found and it can and only 1 file on the server can correspond to it - if my logic is wrong here - Do not do this). Files that should be thrown onto the server are collected in a separate array. After checking the verification of files, you process the array with the files that you need to fill. That's basically it.

    Even as you can do so that the pat so do not carousel. If you can upload files to the server only (for which you write a script), then on the locale with a folder I would create a txt file, where I would write about file changes on the locale, and then launch the script that reads this file and uploads something that , at the end he overwrites it, that is, there should not be lines

    • The first version I did, through the accounting of files on the locale and server in the local txt database. In general, everything turned out, the code turned out just great. Then I tried to implement it through md5 without creating databases, the code is noticeably shorter and simpler. Thanks for the advice, it means that I thought correctly, which I was very pleased with. - northmule