1) Changing the name of the function from checkIfFileExists to fileExists
2) Closing the directory file handle

Now to check if a file exists, use like below:
        if fileExists(filename)