in reply to taking file from user

> "gives wrong file name which does not belong in a directory"

let see... what if your file is a link in another directory pointing to a correct filename in the correct directory?

it depends on what do you understand by wrong filename. A wrong filename and a wrong file are very different things

If you want to validate your file think instead about an "open file or die/warn/try again/plan B" line