PHP + MongoDB + GridFS
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了PHP + MongoDB + GridFS,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含1745字,纯文字阅读大概需要3分钟。
内容图文
6.3.3. MongoDB
6.3.3.1. 安装
sudo pecl install mongo
vim /srv/php/etc/conf.d/mongo.ini extension=mongo.so
6.3.3.2. GridFS
6.3.3.2.1. 上传文件
<?php $connection = new MongoClient( "mongodb://neo:chen@192.168.6.1" ); $db = $connection->test; $grid = $db->getGridFS(); $id = $grid->put("/etc/passwd");
6.3.3.2.2. 读文件
<?php $connection = new MongoClient( "mongodb://neo:chen@192.168.6.1" ); $db = $connection->test; $grid = $db->getGridFS(); $id = $grid->put("/etc/passwd"); $oid = new MongoId($id); $file = $grid->get($oid); echo $file->getBytes();
6.3.3.2.3. storeFile
storeFile 与 put 类似
<?php $connection = new MongoClient( "mongodb://neo:chen@192.168.6.1" ); $db = $connection->test; $grid = $db->getGridFS(); $storedfile = $grid->storeFile("/etc/passwd", array("date" => new MongoDate())); // Return newly stored file‘s Document ID echo $storedfile;
6.3.3.2.4. 查找文件
findOne 与 get 类似,get 只能通过id取出文件,findOne可以查找文件名,日期,尺寸,以及md5值。
<?php $connection = new MongoClient( "mongodb://neo:chen@192.168.6.1" ); $db = $connection->test; $grid = $db->getGridFS(); $file = $grid->findOne("/etc/passwd"); echo $file->getBytes();
指定 collections
<?php $images = $db->mydb->getGridFS(‘images‘); $image = $images->findOne(‘jwage.png‘); header(‘Content-type: image/png;‘); echo $image->getBytes(); ?>
6.3.3.2.5. 遍历文件
findOne一直只返回一条数据,find可以返回结果集,实现遍历文件。
<?php $connection = new MongoClient( "mongodb://neo:chen@192.168.6.1" ); $db = $connection->test; $grid = $db->getGridFS(); $files = $grid->find(); foreach ($files as $file){ print_r($file); }
本文节选自《Netkiller PHP 手札》
http://netkiller.github.io/php/index.html
原文:http://my.oschina.net/neochen/blog/505829
内容总结
以上是互联网集市为您收集整理的PHP + MongoDB + GridFS全部内容,希望文章能够帮你解决PHP + MongoDB + GridFS所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。