Will changing a file name affect the MD5 Hash of a file?

Solution 1:

No, the hash is of the file contents only. You can see this in the source for md5sum and its MD5 implementation. You can also test this if you have access to md5sum:

$ echo "some arbitrary content" > file1
$ cp file1 file2
$ md5sum file1
f0007cbddd79de02179de7de12bec4e6  file1
$ md5sum file2
f0007cbddd79de02179de7de12bec4e6  file2
$

Solution 2:

The usual definition of "MD5 hash of a file" is that the hash is based on the file contents. The name can be freely changed.

$hash1 = md5(file);
// change file name
$hash2 = md5(file);

The two hash codes will be the same.

In some (fairly specialized) use cases, file metadata (name, time stamp(s), etc.) are part of the data used to compute the hash. Then

$hash1 = md5(file);
// change file name
$hash2 = md5(file);

will produce two separate hashes.