使用sql server和php在数据库中记录图片

发布于 2024-12-12 03:52:52 字数 1398 浏览 0 评论 0原文

我的问题是这样吗:

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

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 

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 

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

岛歌少女 2024-12-19 03:52:52

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

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

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();
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文