【问题标题】:recording a picture in database with sql server and php使用 sql server 和 php 在数据库中记录图片
【发布时间】:2011-12-17 04:23:05
【问题描述】:

有我的问题吗:

我用这种方法在数据库中记录了一张图片:

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

我是这样读取这些数据的:

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

另一方面,我尝试直接从 Php 脚本中读取此图片文件,如下所示:

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

事实上,第一种方法(来自数据库)放置了一些八位字节('0')并损坏了我的图片,如下所示。

有谁知道为什么要把这个八位字节放在那里?插入查询是否正确完成?

非常感谢您的帮助!

系统信息:

  • SQL Server 2005
  • PHP
  • IIS

好:

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 

不好:

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 

【问题讨论】:

标签: php sql-server bulkinsert


【解决方案1】:

我将二进制数据的十六进制值存储在数据库中。一旦我阅读它,我就会阅读十六进制并将其转换为二进制。

 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();
    }

【讨论】:

    猜你喜欢
    • 2013-05-31
    • 2017-06-21
    • 1970-01-01
    • 2011-07-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-02
    相关资源
    最近更新 更多