PHP如何复制大文件

顺便说下 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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值