php斷點續傳之如何分割合并檔案_PHP教程

來源:互聯網
上載者:User
複製代碼 代碼如下:
ini_set("memory_limit", "50M");//必須的,根據你環境的實際情況盡量大,防止報錯
ini_set("max_execution_time", "100");
//file_exists() 函數檢查檔案或目錄是否存在,存在則返回 true,否則返回 false。
//fread() 函數讀取檔案(可安全用於二進位檔案)。fread() 從檔案指標 file 讀取最多 length 個位元組。
//filesize() 函數返回指定檔案的大小(位元組)。本函數的結果會被緩衝。請使用 clearstatcache() 來清除緩衝。
$orgFile = 'Fireworks8-chs.exe';//源檔案
$cacheFileName = 'vbcache';//分割成的臨時檔案塊
function cutFile($fileName,$block) {//分割
global $cacheFileName;
if (!file_exists($fileName)) return false;
$num = 1;
$file = fopen($fileName, 'rb');
while ($content = fread($file,$block)) {
$cacheFile = $cacheFileName . $num++ . '.dat';
$cfile = fopen($cacheFile, 'wb');
fwrite($cfile, $content);
fclose($cfile);
}
fclose($file);
}
function mergeFile($targetFile) {//合并
global $cacheFileName;
$num = 1;
$file = fopen($targetFile, 'wb');
while ($num > 0) {
$cacheFile = $cacheFileName . $num++ . '.dat';
if (file_exists($cacheFile)) {
$cfile = fopen($cacheFile, 'rb');
$content = fread($cfile, filesize($cacheFile));
fclose($cfile);
fwrite($file, $content);
}
else {
$num = -1;
}
}
fclose($file);
}
//調用
cutFile($orgFile, 10 * pow(2,20)); //10 * pow(2,20) 就等於 10M pow() 函數返回 x 的 y 次方
mergeFile('ok.exe');
?>

http://www.bkjia.com/PHPjc/744708.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/744708.htmlTechArticle複製代碼 代碼如下: ?php ini_set("memory_limit", "50M");//必須的,根據你環境的實際情況盡量大,防止報錯 ini_set("max_execution_time", "100"); //file_exi...

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.