Ah. That's easy--when you said:
$tmpfile="/tmp/tmpfile.jpg";
fp = fopen($tmpfile,"w");
the file named in the variable $tmpfile either doesn't exist, or you don't have the rights to open it for writing, so the call to fopen() fails, returning a magic value that means "not a valid file handle".
In other words (don't know how I missed this) you should really be saying:
if ($fp = fopen(...))
{
// write something to the file
}
else
echo "Can't open file";