Why does this happen and, most importantly, how to avoid this?
You should ask this question on Security.SE.
To avoid corruption you should make backups.
In information technology, a backup, or the process of backing up,
refers to the copying and archiving of computer data so it may be used
to restore the original after a data loss event. The verb form is to
back up in two words, whereas the noun is backup.
Writable digital optical media such as CD-R and DVD-R could be corrupted even by sunlight. To avoid this you might use something like double backup strategy. However, nowadays, we have such beautiful online methods to backup photos, like Instagram or other online services. For example, behind Google Drive there are large amount of data centers. Every one of your photos will be stored on the Google File System (GoogleFS or GFS) on servers hosted in these data centers. This filesystem was designed by Google to protect users from losing data. Your photos will be saved in three exemplars in three different geographic locations, to protect against data loss because of local disasters from lightning, tsunami, earthquake, etc.
Try to use multiple online services simultaneously (such as megaupload, dropbox, wuala, 4shared, etc.).
Thereafter, for the greatest peace-of-mind that none of your photos were modified or corrupted, you would use checksums to verify your data integrity.
A checksum is a small-size datum from a block of digital data for the
purpose of detecting errors which may have been introduced during its
transmission or storage.
The best way is to use cryptography for such task. Use the latest stable hash functions, such as SHA-256 or SHA-512. Older algortihms, such as MD5, are now considered insecure. A modern attacker could make forgery of your data with the same md5sum.
When you are done, you will have a distributed backup system which will not be easily broken, even by the most sophisticated and dedicated attackers (such as nation state -backed attacks).
Two DVD copy with identical datas inside:
Where this SHA256SUM shall contain something like:
209732fbdb499f0cad6fd3... are unique hashes of your photos. When one bit of your photo becomes corrupt, this hash will be changed to another, so you could test hashes against this list to be calm that your photos are keeping integrity.
Secondly, all of this photos and this SHA256SUM-hashes file are uploaded to e.g. dropbox and somewhere else.
Now, you have 5 copies of your photos. 2 off-line, 2 on-line, 1 on your current hard drive.
The scheme might be evolved to something more robust of course. Your imagination is your trump card.