<?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';
http://php.net/manual/zh/function.fread.phpphp