Question

I'm using the Apache Commons NET and their FTPClient.

Now my case: I've got a file on the server and the same file in the same hierarchy locally. On the server, I generate the MD5-Checksum of the file using PHP's md5_file(). On my machine, I am generating the MD5-Checksum using Apache Commons Digest Utils md5hex().

Those MD5-Hashes actually quite always matches - except for certain files.

One of the file is the current jQuery-Library. Another of these files is a .ttf Font-File and another one is a regular PHP file (system/libraries/Migration.php from CodeIgniter).

Now the amazing part: If I see that those MD5 Checksums are not the same, I upload the local file to the server (or I download the FTP-File to my computer, depends on which one is newer (timestamp)). But no matter what, after the Up- or Download, the MD5 Checksums are still not the same - from exactly the same files. Why?

As I thought, the timestamp could be part of the md5-data, but this is actually impossible because then all the other files would fail too.

I'd appreciate your help!

Was it helpful?

Solution

As Jon Skeet already mentioned, the usual reason why this happens is that the files are not transferred in binary, but in ASCII mode.

This is supported by the FTPClient documentation for FTPClient.setFileType(), which states:

The default file type is FTP.ASCII_FILE_TYPE if this method is never called.

To transfer your file in binary mode using FTPClient, call the setFileType before uploading:

FTPClient.setFileType(FTP.BINARY_FILE_TYPE);

The session will stay in binary mode until you change it again.

By the way, timestamps are not an issue, they do not affect the MD5 hash.

OTHER TIPS

I don't have comment previlleages so that i am posting as an answer. I have faced this earlier in one of my projec, FTP clients used to add newline characters in ASCII mode.

Refer the below link for more info https://superuser.com/questions/39520/downloading-files-with-filezilla-result-in-newline-r-n-n

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top