PHP is truncating MSSQL Blob data (4096b), even after setting INI values. Am I missing one?

I am writing a PHP script that goes through a table and extracts the varbinary(max) blob data from each record into an external file. The code is working perfectly (I used virtually the same code to go through some images) except when a file is over 4096b – the data is truncated at exactly 4096.

I’ve modified the values for mssql.textlimit, mssql.textsize, and odbc.defaultlrl without any success.

Am I missing something here?

 ini_set("mssql.textlimit" , "2147483647");
 ini_set("mssql.textsize" , "2147483647");
 ini_set("odbc.defaultlrl", "0");

 include_once('common.php'); //Connection to DB takes place here.
 $q = odbc_exec($connect, "Select id,filename,documentBin from Projectdocuments where id = $id"); 
 if (odbc_fetch_row($q)){

  echo "Trying $filename ... ";

  if (file_exists($fileName)){

     if($fh = fopen($fileName, "wb")) {
         fwrite($fh, $binData) ;
         $size = filesize($fileName);
         echo ("$fileName<br />Done ($size)<br><br>");
     }else {
      echo ("$fileName Failed<br>");


Trying … projectPhotos/docs/file1.pdf
Done (4096)

Trying … projectPhotos/docs/ Done (4096)

Trying …
projectPhotos/docsv3.pdf Done (4096)


Here is Solutions:

We have many solutions to this problem, But we recommend you to use the first solution because it is tested & true solution that will 100% work for you.

Solution 1

Instead of setting odbc.defaultlrl to 0, try setting it to an actual value instead:

ini_set("odbc.defaultlrl", "100K");

Solution 2

If you’re using mssql (freetds), look in /etc/freetds.conf for a setting called “text size”. Mine was set to 64512 and that’s exactly what my images were being truncated to. I set it to 5MB (5242880) and it’s working like a charm now.

text size = 5242880

Solution 3

According to this comment in the manual, you must set the INI settings before connecting, which does not seem to be your case.

Solution 4

I know this is ancient, but I solved this differently. ini_set does not wok for mssql.textlimit or mssql.textsize, this is documented on

Both of these default to 4096b (4k) in the php.ini file. Reset these to a higher value and it will work fine.

Dont forget to restart httpd service after.

Note: Use and implement solution 1 because this method fully tested our system.
Thank you 🙂

All methods was sourced from or, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply