1

Using a custom DevExpress application our users are uploading PDF files which get stored in a VARBINARY(MAX) column on a MSSQL 2008 database.

I have LAMP box which successfully connects to that database using the FreeTDS driver.

I'm able to retrieve other types of information (images stored as blobs, dates, strings etc) but when I try to serve PDFs they become corrupted somehow.

If I do a comparison of the file before upload and after download using a hex editor I can see they are different (the string in the after shot matches what is on the db 128B08...)

Before and after

The PHP I am using to serve the file:

<?php
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: public"); 
header("Content-Description: File Transfer");
header("Content-type:application/pdf");
header("Content-Disposition:attachment;filename=" . $arr[0]['FileName']);
header("Content-Transfer-Encoding: binary");
echo $arr[0]['FileContent'];

The C# used to save the file to the db:

public void LoadFromStream(string fileName, Stream stream)
{
  Guard.ArgumentNotNull(stream, "stream");
  Guard.ArgumentNotNullOrEmpty(fileName, "fileName");
  FileName = fileName;
  byte[] bytes = new byte[stream.Length];
  stream.Read(bytes, 0, bytes.Length);
  Content = bytes;
}

public void SaveToStream(Stream stream)
{
  if (string.IsNullOrEmpty(FileName))
  {
    throw new InvalidOperationException();
  }
  stream.Write(Content, 0, Size);
  stream.Flush();
}

public byte[] Content
{
  get { return GetDelayedPropertyValue<byte[]>("Content"); }
  set
  {
    int oldSize = size;
    if (value != null)
    {
      size = value.Length;
    }
    else
    {
      size = 0;
    }
    SetDelayedPropertyValue<byte[]>("Content", value);
    OnChanged("Size", oldSize, size);
  }
}

I've read just about every article I can find by searching "php varbinary, php output stream, php varbinary stream, varbinary encoding". Help or suggestions much appreciated!

2
  • Looks like your downloaded file contains less than half the bytes of the original file. What does the byte array look like in the database before you pull it back in your PHP code? Commented May 16, 2012 at 10:26
  • @JamieDixon the db contains 5414 characters. Here is a zip containing the before and after pdfs, as well as a txt file with the content of the db. I deliberately created empty pdfs to keep the file size down. Commented May 16, 2012 at 10:47

1 Answer 1

2

There's a couple of problems with this.

First off, the data stored in the database is in Hex format so you'll need to convert that back to a byte array in your PHP code before serving it to the user.

Secondly, the hex reprisentation you've got in the database doesn't seem to be correct for the PDF you're using.

When I converted the PDF from a byte array to a hex reprisentation I got a very different looking Hex string that when converted back to a byte array, works fine.

Sign up to request clarification or add additional context in comments.

1 Comment

I'm accepting this as the correct answer for pointing out that the byte array contains about half the bytes of the original file. Going back to the DevExpress developers revealed that they are using CompressionConverter when manipulating the stream.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.