[PHP学习教程 - 文件]001.高速读写大数据“二进制”文件,不必申请大内存(Byte Block)
2015-09-09 11:43
736 查看
引言:读写大“二进制”文件,不必申请很大内存(fopen、fread、fwrite、fclose)!做到开源节流,提高速度!
每天告诉自己一次,『我真的很不错』....
加速读写大文件,在实际工作过程当中其实想必很多人都有这样的经历-大家知道,如果使用记事本(notepad)打开10M的文本文件,那会卡到无响应,但是如果使用Sublime或者Notepad++则瞬间打开。
不展开讲了,接口简单,多说无益,直接上码。
[b]函数
[/b]
<?php /** * 读写大二进制文件,不必申请很大内存 * 只有读取到内容才创建文件 * 保证目录可写 * @param string $srcPath 源文件路径 * @param string $dstPath 目标文件路径 * @return bool */ function fetch_big_file($srcPath, $dstPath) { // 设置脚本执行时间无限长 set_time_limit(0); if (!$fpSrc = fopen($srcPath,"rb")) { return false; } // 写文件 - 是否已打开? $isWriteFileOpen = false; do { // 每次读取 8*1024个字节 $data = fread($fpSrc, 8192); if (!$data) { break; } else if (!$isWriteFileOpen) { // 第一次读取文件,并且有内容,才创建文件 $fpDst = fopen($dstPath,"wb"); $isWriteFileOpen = true; fwrite($fpDst, $data); } else { // 写入 fwrite($fpDst, $data); } } while (true); fclose($fpSrc); fclose($fpDst); return true; } // 功能示例: $srcPath = 'd:/PHP/data/eclipse-jee-kepler-R-win32-x86_64.pdf'; $dstPath = 'Z:/reslibCovertingfiles/eclipse-jee-kepler-R-win32-x86_64.pdf'; fetch_big_file($srcPath, $dstPath); echo 'success';
各位看官,如果有误,请无情指出!
未完待续……。
本站文章为 宝宝巴士 SD.Team 原创,转载务必在明显处注明:(作者官方网站: 宝宝巴士 )
转载自【宝宝巴士SuperDo团队】 原文链接: /article/6669176.html
相关文章推荐
- [大数据处理]-如何用消息系统避免分布式事务?
- 【重磅】纲要已出 大数据行业全面解析
- HDU4355-Party All the Time-三分法(模板)-2012 Multi-University Training Contest 6
- shutdownServer failed: shutdown must run from localhost when running db without auth
- 国务院:《促进大数据发展行动纲要》-全文
- DS | Tree | PAT | Tree Traversals Again
- hashCode和equals重写
- Error: Activity class {package1/package2.mainActivity} does not exist.
- 大数据工具和数据分析资料汇总
- LeetCode_contains-duplicate
- 大数据的商业模式
- rails db:create错误
- New package not yet registered with the system. Waiting 3 seconds before next attempt
- POJ 1995 Raising Modulo Numbers(快速幂)
- raid write-bakc write-through的解释
- raid组的初始化详解
- Contains Duplicate
- 游戏人工智能开发之6种决策方法
- 游戏人工智能开发之6种决策方法
- oschinaIM/聊天/语音工具