我正在使用GridFS,并且我目前已经使用findOne来显示单个图像,但我希望它遍历网格中的所有结果并将它们全部回显到屏幕,这里是我使用的代码:迭代遍历结果MongoDB和GridFS(PHP)
<?php
try {
// open connection to MongoDB server
$conn = new Mongo;
// access database
$db = $conn->database;
// get GridFS files collection
$grid = $db->getGridFS();
// retrieve file from collection
header('Content-type: image/png');
$file = $grid->findOne(array('_id' => new MongoId('4fb437dbee3c471b1f000001')));
// send headers and file data
echo $file->getBytes();
exit;
// disconnect from server
$conn->close();
} catch (MongoConnectionException $e) {
die('Error connecting to MongoDB server');
} catch (MongoException $e) {
die('Error: ' . $e->getMessage());
}
?>
感谢
我试过这个$ files = $ grid-> find(); foreach($ files as $ file){echo $ file-> getBytes(); }虽然它不起作用 –
如果你想要所有的文件,试试find({})(注意大括号为空参数)。而不是显示$ file-> getBytes();在你的循环中,尝试“print_r($ file);”只是为了调试,看看你是否有任何东西。 – jbnunn