【发布时间】:2013-12-27 22:07:13
【问题描述】:
我正在尝试使用 GridFS 将图像保存到 MondoDB 集合。为了测试这一点,我做了以下操作:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
</head>
<body>
<?php
// open connection
$m = new MongoClient();
$db = $m->selectDB('ImageDatabase');
$gridUploads = $db->getGridFS('images');
// save image
$fileName = 'C:\Users\Thomas\Pictures\commits.png';
$gridUploads->storeFile($fileName);
// load image
$doc = $gridUploads->findOne($fileName);
// dsplay image
header('Content-type: image/png');
echo $doc->getBytes();
?>
</body>
</html>
这应该将图像上传到集合中,然后从集合中获取相同的图像并显示它。
这之后的合集内容是:
{
"_id" : ObjectId("52a6fcdd1dc38f0c3c0016bf"),
"filename" : "C:\\Users\\Thomas\\Pictures\\commits.png",
"uploadDate" : ISODate("2013-12-10T11:37:01.000Z"),
"length" : 179952,
"chunkSize" : 262144,
"md5" : "768d618923442668ca2a60f02be59d52"
}
print_r($doc):
MongoGridFSFile Object
(
[file] => Array
(
[_id] => MongoId Object
(
[$id] => 52a6fcdd1dc38f0c3c0016bf
)
[filename] => C:\Users\Thomas\Pictures\commits.png
[uploadDate] => MongoDate Object
(
[sec] => 1386675421
[usec] => 0
)
[length] => 179952
[chunkSize] => 262144
[md5] => 768d618923442668ca2a60f02be59d52
)
[gridfs:protected] => MongoGridFS Object
(
[w] => 1
[wtimeout] => 10000
[chunks] => MongoCollection Object
(
[w] => 1
[wtimeout] => 10000
)
[filesName:protected] => images.files
[chunksName:protected] => images.chunks
)
[flags] => 0
)
但是结果的正文内容是:
<img style="-webkit-user-select: none" src="http://localhost/path-to/the-script.php">
有什么想法吗?
谢谢!
【问题讨论】:
-
结果内容是图片标签?
-
是的,我不知道实际应该发生什么,但我猜一个img标签,其中src属性是php文件的路径不是它^^
-
我真的不知道这怎么可能......
标签: php image mongodb src gridfs