从数据库读取或写入数据库

从数据库读取或写入数据库

问题描述:

I try to show an image stored in a database. I wrote it with

$image = file_get_contents($testPfad);
$dateigroesse = filesize($testPfad);

$arrData = unpack("H*hex", $image); 
$data_string = "0x".$arrData['hex']; 

$sql = "INSERT INTO EHS.dbo.T_Signaturen (UnterschriftsDateiName,UnterschriftsBild,Dateigroesse,terminID) VALUES (
        '".$unterschriftsFileName."',
        CONVERT(varbinary(max),'$data_string'),
        '".$dateigroesse."',
        '384_234')";
        echo '<hr>'.$sql;
        insert($sql);

With this code I output the image.

header("Content-type: image/jpeg");  
$query = "SELECT Dateigroesse, CONVERT(varchar(max), UnterschriftsBild) as content_data FROM EHS.dbo.T_Signaturen WHERE ID = '10'"; 
$result = query($query);
$content = $result[1]["content_data"];       
$filesize = $result[1]["Dateigroesse"]; 

$content = substr ($content, 2);             // entfernt 0x
$content = pack("H*", $content); 
print $content;  

Everything works fine but only a part of the image is shown. I reduced the image size from 20kb to 2kb and much more is shown so I think ANYWHERE the binary data is cut. The Database Column is a varbinary(max)

Please let us not discuss wether it is rational to store blob in databases :) :)

The solution was to increase the max accepted string length of odbc in php.ini. It was set to 4069 chars

  • did you try to define your column as BLOB (up to 2 GB)?
  • are you sure, that the complete image is stored (maybe that function is wrong)