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:

  1. Function1, searches for all files in the local directory and writes them to the local.baz file as "file name ----- file size"
  2. 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"
  3. Function 3 compares two databases line by line, local.baz and remote.baz, and makes a decision about uploading a file to ftp.
  4. 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"; } 
  • Good idea, thanks. I will try to do it now - as an option. - northmule
  • Compare md5 file sizes. Hashes do not converge - urgently update! - lampa
  • one
    @lampa IMHO, is not universal and only one character can change in the file, and the size does not change. Normal work will be provided that the file is constantly growing in size. @northmule and what is your specific task? What is stored in files? - zenith
  • @lampa take my words back: if the files are very large, it is really easier to compare the hash with the size. - zenith
  • @zenith then compare the date the file was changed. About the size, of course, you are right! - lampa


1 answer 1

Did not penetrate into your code, but try to calculate md5 for files, for example, once an hour. And update those files that have not come together.
In theory, this is faster than a line-by-line comparison and will result in any file changes.