脚本宝典收集整理的这篇文章主要介绍了如何使用PHP在MongoDB上保存文件?,脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。
粘贴一些代码将其包含在答案中:
<?PHP // Connect to Mongo and set DB and Collection $mongo = new Mongo(); $db = $mongo->myfiles; // GriDFS $grid = $db->getGridFS(); // The file's location in the File System $path = "/tmp/"; $filename = "03-smbd-menu-screen.mP3"; // Note Metadata field & filename field $Storedfile = $grid->storeFile($path . $filename,array("Metadata" => array("filename" => $filename),"filename" => $filename)); // Return newly stored file's Document ID echo $storedfile; ?>
要恢复文件:
<?PHP // Connect to Mongo and set DB and Collection $mongo = new Mongo(); $db = $mongo->myfiles; // GridFS $gridFS = $db->getGridFS(); // Find image to stream $image = $gridFS->findOne("chunk-screaming.jpg"); // Stream image to browser header('Content-tyPE: image/jpeg'); echo $image->getBytes(); ?>
以上是脚本宝典为你收集整理的如何使用PHP在MongoDB上保存文件?全部内容,希望文章能够帮你解决如何使用PHP在MongoDB上保存文件?所遇到的问题。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。