PHP 大文件快速复制 stream_copy_to_stream
PHP 流操作 操作stream_copy_to_stream 以流的方式 将 A文件的数据 复制到B 文件里面
目前有一个14.6M的文件

检测 使用内存峰值 :
function formatBytes($bytes, $precision = 2) {$units = array("b", "kb", "mb", "gb", "tb");$bytes = max($bytes, 0);$pow = floor(($bytes ? log($bytes) : 0) / log(1024));$pow = min($pow, count($units) - 1);$bytes /= (1 << (10 * $pow));//round() 函数对浮点数进行四舍五入。return round($bytes, $precision) . " " . $units[$pow];}echo formatBytes(memory_get_peak_usage());
////////////////////////////////////////////
下面是文件内容的复制
$f1 = file_get_contents("txt.txt");$f2 = fopen("txt1.txt",'w+');fwrite($f2, $f1);fclose($f2);

文件是 14M 这时候会发现 尽然占用了16Mb的内存 !!!
而且速度还不是很快 , 关于大文件内存加载可以参考大文件读取
//解决:stream_copy_to_stream 函数可以被用来将一个流中的数据复制到另一个
$f1 = fopen("txt.txt",'r');$f2 = fopen("txt1.txt",'w+');stream_copy_to_stream( $f1,$f2);fclose($f1);fclose($f2);

发现尽然神奇的 用了0.008Kb左右 ,(因为检查使用内存的函数也在占用)
【文章来源】
作者:自由网
来源:CSDN
原文:https://blog.csdn.net/freedom_fd/article/details/90523091
版权声明:本文为博主原创文章,转载请附上博文链接!
