我正在使用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