我目前正在使用两个文件,一个获取内容,第二个显示它(或也尝试)
我需要做的是遍历存储在gridFS中的图像以将它们全部显示,我可以获得要迭代的文件名,并且文件名在find的参数内,但它不起作用,我只是得到一个损坏的图像,这里是代码:
显示.php
<?php
// open connection to MongoDB server
$conn = new Mongo;
// access database
$db = $conn->database;
// get GridFS files collection
$grid = $db->getGridFS();
$cursor = $grid->find();
foreach ($cursor as $obj) { // iterate through the results
$filename = $obj->getFilename().'<br/>';
echo "<img src='newupload.php?filename=".$filename."'>";
echo $filename;
}
?>
这是进入gridFS的那个
<?php
$filename = $_GET['filename'];
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->find(array('filename' => $filename ));
// 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());
}
?>
非常感谢