In general, this situation. Connecting to a remote ftp. I go to the directory and get a list of all the files. I climb on a lokalka, I receive an array with files which is I Compare them, and which are not loading. But there are files that have been updated, they also need to be downloaded. I do it with the md5_file function. There is no problem getting the local file hash. But the remote server has a problem. How I implement it all.

public function connect($action, $img_name = null) { //подключаемся if (@!$conn_id) { $conn_id = ftp_connect($this->ftp_server); } ftp_login($conn_id, $this->ftp_user_name, $this->ftp_user_pass); switch ($action) { case 'xls': $this->changeDirectory($conn_id, "FE"); $list = $this->getFileCatalog($conn_id); $this->loadXls($conn_id); break; case 'img': if (ftp_pwd($conn_id) !== '/FE/image') { $this->changeDirectory($conn_id, "FE"); $this->changeDirectory($conn_id, "image"); } $list = $this->getFileCatalog($conn_id); $images_list = $this->getListImages(); $for_load = array_diff($list, $images_list); if (count($for_load) > 0) { $this->imageLoad($conn_id, $for_load); } break; } ftp_close($conn_id); } public function changeDirectory($conn_id, $name) { if (ftp_chdir($conn_id, $name)) { echo "Новая текущая директория: " . ftp_pwd($conn_id) . "\n"; } else { echo "Не удалось сменить директорию \n"; } } public function getFileCatalog($conn_id) { $list = ftp_nlist($conn_id, "."); return $list; } public function getListImages() { $dir = '/путь/'; return $files2 = scandir($dir, 1); } public function VerifyKeyFile($conn_id,$inputname, $locImage) { $dir = '/путь/'; if (md5_file($dir . $locImage) == md5_file($_FILES["$inputname"])) { return true; } else { return false; } } public function loadXls($conn_id) { $local_path_file = dirname(__DIR__) . $this->temp . $this->name_local_file; if (ftp_get($conn_id, $local_path_file, $this->server_file, FTP_BINARY)) { echo "Успешно загрузили данные в $this->name_local_file \n"; } else { echo "Проблемы с загрузкой файла \n"; } } public function imageLoad($conn_id, array $img_name) { foreach ($img_name as $img) { $local_path_file = '/путь/' . $img; if (ftp_get($conn_id, $local_path_file, $img, FTP_BINARY)) { echo "Успешно загрузили изображение в $img \n"; } else { echo "Проблемы с загрузкой файла \n"; } } } 

    1 answer 1

    FTP protocol does not have the command to get the md5 hash of the remote file. Therefore, to get it, you need to download the file from the server and calculate its hash. Or, as an option, store hashes on the server along with the files. Something like that

    • readme.txt
    • readme.txt.md5
    • Yes, thanks, I already read about this. Made it a different way, in size, but it may give an oversight, albeit with a small probability - mydls1