I'm not sure what you're trying to do. The code you have there gets an attachment record from the database but I don't see where you're trying to get the actual file data. You could look at attachment.php. In fact you may be able to use attachment.php to get the data for you instead of figuring out the details.