Look at the source of the JPEG as it is when downloaded using your script (open the JPEG with notepad, wordpad, TextEdit, or another text editor of your choice). If you don't immeditately see what's wrong, compare the file with the original by opening the orignal in a text editor as well. Sometimes you might have a space character inserted at the front, or maybe you have magic quotes enabled and PHP has inserted slashes into the raw source. stripslashes will correct that in most cases, if disabling magic quotes isn't an option.
HTH!
Regards,
Rich
--
Author,
Beginning CSS: Cascading Style Sheets For Web Design
CSS Instant Results
http://www.catb.org/~esr/faqs/smart-questions.html