shouldn't you strip any directory that was specified in the filename, and just keep the base name as filename?