PHP如何复制大文件

版权声明:转载请注明出处 https://blog.csdn.net/liuxl57805678/article/details/90167948

顺便说下 PHP 复制小文件,直接使用 copy 函数就OK了

话不多说,直接上代码

function copy_file($path, $to_file) {

    if (!is_readable($path))
	return false;

    if (!is_dir(dirname($to_file)))
	@mkdir(dirname($to_file).'/', 0747, true);

    $handle1 = fopen($path, 'r');
    $handle2 = fopen($to_file, 'w');
    if ($handle1 && $handle2) {
	stream_copy_to_stream($handle1, $handle2);
	fclose($handle1);
	fclose($handle2);
    }
}
关于stream_copy_to_stream,请参考:https://php.net/manual/zh/function.stream-copy-to-stream.php

如果你对stream_copy_to_stream函数持有怀疑态度,可以用 memory_get_peak_usage 和 microtime 去测一下代码的占用内存和运行时间。

https://php.net/manual/en/function.memory-get-peak-usage.php

猜你喜欢

转载自blog.csdn.net/liuxl57805678/article/details/90167948