Kohana PHP、ORM 和 MySQL BLOB
我正在尝试通过 Kohana 的 ORM 库在 MySQL 表中创建和检索 BLOB。
代码看起来像:
$attachment = new Attachment_Model();
$attachment->name = $info['FileName'];
$attachment->size = strlen($info['Data']);
$attachment->data = $info['Data'];
$attachment->mime_type = $info['content-type'];
$attachment->save();
我已经通过将数据输出到文件来验证此时数据是否正常。 但是,当我检索数据时,它已损坏。 我已经设法缩小范围 - 我使用 MySQL 查询工具提取数据库中保存的数据,并且我可以验证数据库中的数据是否已损坏,因此问题一定出在 INSERT 上。
此外,输入的文件并不总是损坏的 - 较小的文件(例如图像)往往没问题。
有人有主意吗?
I'm trying to create and retrieve a BLOB in a MySQL table via Kohana's ORM library.
The code looks something like:
$attachment = new Attachment_Model();
$attachment->name = $info['FileName'];
$attachment->size = strlen($info['Data']);
$attachment->data = $info['Data'];
$attachment->mime_type = $info['content-type'];
$attachment->save();
I've verified that the data is OK at this point by outputting it to a file. However, when I retrieve the data it comes out corrupted. I've managed to narrow this down a bit more - I've used the MySQL query tool to extract the data as held in the database and I can verify that the data in the database is corrupt, so the problem must be on the INSERT.
Also, the files inputted aren't always corrupt - smaller files (such as images) tend to be OK.
Anyone have any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
事实证明,在本例中,我使用的是 BLOB 数据类型。
BLOB 数据类型将数据截断为 65535 个字符(静默地,不会引发错误!)
我已将其升级为 MEDIUMBLOB(最大长度为 16777215 个字符),并且它似乎工作正常!
It turns out that, in this case, I was using the BLOB data type.
The BLOB data type truncates data at 65535 characters (silently, without throwing an error!)
I've upped it to a MEDIUMBLOB (which has a max length of 16777215 characters), and it seems to work OK!
大胆猜测,但是:可能是因为 kohana 模型层将所有数据作为字符数据而不是二进制数据插入,这会给您在保存/检索 BLOB 对象时带来麻烦。
Wild guess, but: probably because the kohana model layer inserts all data as character data instead of binary, which will cause you troubles when saving/retrieving BLOB objects.