Home > php教程 > php手册 > body text

PHP读写大“二进制”文件,不必申请很大内存(fopen、fread、fwr

WBOY
Release: 2016-06-06 19:39:21
Original
1431 people have browsed it

PHP读写大“二进制”文件,不必申请很大内存(fopen、fread、fwrite、fclose)(转) 纠正: PHP下载/采集远程图片到本地:http://www.oschina.net/code/snippet_1398542_50533 ?php/** * 读写大二进制文件,不必申请很大内存 * 只有读取到内容才创建文件 *

PHP读写大“二进制”文件,不必申请很大内存(fopen、fread、fwrite、fclose)    (转)

纠正:
PHP下载/采集远程图片到本地 :http://www.oschina.net/code/snippet_1398542_50533

<?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
	{
		$data = fread($fpSrc, 8192); // 每次读取 8*1024个字节
		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';
Copy after login

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!