Task: There are local files (they can change), there is a folder on ftp. It needs to be implemented so that synchronization between local files and ftp occurs. Ie if the file has not changed, then skip it. If changed - download. If there is a new one - download. Implemented: I wrote several functions, schematically like this:
- Function1, searches for all files in the local directory and writes them to the local.baz file as "file name ----- file size"
- Function2 does the same thing only with ftp files, creates a remote.baz file locally and writes there information on all files on ftp as "file name ----- file size"
- Function 3 compares two databases line by line, local.baz and remote.baz, and makes a decision about uploading a file to ftp.
- Function4 upload to ftp
Perhaps I am wise because I am following a php programmer in a thorny way (self-taught). There is such a trap that function 3 accurately determines that the file from local.baz is in the file remote.baz, but still performs the function of downloading to ftp. Why so, I do not understand, tell me - who is strong. Or give an idea for easier implementation than I came up with. A piece of code where there is a condition and the decision to download to ftp
function sravnenie_remot_local($file_name_local, $file_name_remote) { $rename_pravilo = "/[a-zA-Z0-9]+\.[a-zA-Z0-9]+/"; if ($handle_local1 = fopen($file_name_local, "r")) echo "файл $file_name_local открыт"; if ($file_local_mas = fread($handle_local1, filesize($file_name_local))) echo "<br> прочитал $file_name_local<br>"; //получаем массив содержимого файлов $fopen_files_local = file($file_name_local); $fopen_files_remote = file($file_name_remote); //цикл обработки локальной базы расширения .baz foreach ($fopen_files_local as $v_loc) { echo " --локальный ".$v_loc."<br>"; //вложенный цикл обработки удалённой базы foreach ($fopen_files_remote as $v_rem) { echo "Сравниваем файлы<br>"."--удаленный ".$v_rem."<br>"; $ravno=strcmp($v_loc, $v_rem); if ($ravno!==0) { echo "<br>$v_loc=НЕ РАВНО=$v_rem<br>"; echo "<br>Если вижу значит условие прошло<br>"; //Обратное преобразование имени с размером в обычное имя preg_match($rename_pravilo, $v_loc, $v_loc_rename); foreach ($v_loc_rename as $v_loc_final_name) { //загрузка на ftp ftp_nod_upload($v_loc_final_name); echo "Файл $v_loc_final_name загружен ---- Условие завершено<br>"; } } else { echo "Обратное условие началось<br>Файл $v_loc_final_name <strong>ПРОПУЩЕН</strong><br>"; } } } echo "<br>Загрузка завершена<br>"; ftp_close($antona_connect); if ($handle_remote11 = fopen($file_name_remote, "r")) echo "<br> Файл $file_name_remote окткрыт "; if (fread($handle_remote11, filesize($file_name_remote))) echo "<br> прочитал $file_name_remote"; }