我需要一份php脚本,用于从url到服务器的可恢复文件下载。它应该能够启动下载,然后当它中断(30秒至5分钟)时恢复下载,直到完成整个文件。
在perl中有类似的东西 http://curl.haxx.se/programs/download.txt ,但我想在php中实现它,我不会perl。
我认为可以使用
这是我的尝试:
在perl中有类似的东西 http://curl.haxx.se/programs/download.txt ,但我想在php中实现它,我不会perl。
我认为可以使用
CURLOPT_RANGE
下载块,并使用fopen($fileName, "a")
将其附加到服务器上的文件。这是我的尝试:
<?php
function run()
{
while(1)
{
get_chunk($_SESSION['url'], $_SESSION['filename']);
sleep(5);
flush();
}
}
function get_chunk( $url, $fileName)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
if (file_exists($fileName)) {
$from = filesize($fileName);
curl_setopt($ch, CURLOPT_RANGE, $from . "-");//maybe "-".$from+1000 for 1MB chunks
}
$fp = fopen($fileName, "a");
if (!$fp) {
exit;
}
curl_setopt($ch, CURLOPT_FILE, $fp);
$result = curl_exec($ch);
curl_close($ch);
fclose($fp);
}
?>