1

Is there my problem:

I have a picture recorded in database with this method:

UPDATE myTable 
SET pictureData = (SELECT * FROM OPENROWSET(BULK 'myFileAdress.jpeg', SINGLE_BLOB)AS x ) 
WHERE …

I read this data like this:

$myData = $myConnection->query('Select pictureData from myTable where …');
$row = $myData->fetch(PDO::FETCH_ASSOC);
echo @pack('H*', $row['pictureData'])

In another hand I try directly read this picture file from a Php script as following:

$data = fopen ($myPictureAdress, 'rb');
$size = filesize ($picture);
echo fread ($data, $size);

In fact some octet ('0') are placed with the first method (from the database) and corrupt my picture as seen below.

Does anyone knows why this octets are placed there ? Do the insert query is right done?

Thanks a lot for your help!

System information:

  • SQL Server 2005
  • Php
  • IIS

good:

000000D0   38 00 42 00 63 00 63 00  63 00 63 00 63 00 63 00   8 B c c c c c c <-- no error
000000E0   63 00 63 00 63 00 63 00  63 00 63 00 63 00 63 00   c c c c c c c c 
000000F0   63 00 63 00 63 00 63 00  63 00 63 00 63 00 63 00   c c c c c c c c 
00000100   63 00 63 00 63 00 63 00  63 00 63 00 63 00 63 00   c c c c c c c c 

bad:

00000100   63 00 63 00 63 00 63 00  63 00 06 00 36 00 36 00   c c c c c   6 6 <-- error
00000110   36 00 36 00 36 00 36 00  36 00 36 00 36 00 36 00   6 6 6 6 6 6 6 6 
00000120   36 00 36 00 36 00 36 00  36 00 36 00 36 00 36 00   6 6 6 6 6 6 6 6 
skaffman
  • 398,947
  • 96
  • 818
  • 769
user954156
  • 468
  • 2
  • 6
  • 17
  • I am not familiar with the method how you insert the data into sql. but maybe you find some hints and links at this entry: http://stackoverflow.com/questions/6106470/php-convert-a-blob-into-an-image-file/6106541#6106541 – rokdd Oct 29 '11 at 11:00

1 Answers1

0

I Store Hex value of the binary data in the Database . once i read it i read the hex and convert it to binary.

 public byte[] StringToByteArray(string hex)
 {
 return Enumerable.Range(0, hex.Length)
                         .Where(x => x % 2 == 0)
                         .Select(x => Convert.ToByte(hex.Substring(x, 2),16))
                         .ToArray();
    }
Shadi Zidan
  • 154
  • 5