MS SQL Blob Truncated

A frustrating debugging session. Reading JPG files stored as blobs in MS SQL, outputting them with PHP headers. About one-third of images were corrupted—appearing to stop at random points.

Random corruption is the worst kind. No pattern means no clue.

The Discovery

Then I noticed: all broken images were exactly 64,512 bytes.

That’s suspiciously close to 2^16 (65,536). Not random at all. A configuration limit.

The Cause

The FreeTDS configuration file (freetds.conf) has a text size parameter. It was capped at 64,512 bytes by default. Any blob larger than that got silently truncated.

The Fix

For FreeTDS users, increase the text size in freetds.conf:

[global]
text size = 20971520

Restart the web server after changing the configuration.

If you’re using the MSSQL extension instead of FreeTDS, check the mssql.textsize parameter in php.ini. It defaults to only 4 kilobytes—even more restrictive.

mssql.textsize = 20971520

The Lesson

“Random” failures often aren’t random. When data corruption hits a suspiciously round number, look for configuration limits.

The images weren’t corrupted. They were truncated. Once you know the real cause, everything makes sense.