Just a quick question. Why are you opening the file in the first place? Since this is a CGI script I would bet you are viewing this script via a web browser in which case you could simply check for the existence of the file, if its there pass the path(url) to the browser else error out.
Just a thought since from looking at your code above you are not really doing anything with the image that requires reading it in binarily (is that a word?).
Of course I have not seen the rest of your code so I could be wrong.