1.获得url的文件流
function _curl_get($url) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);// 返回字符串,而非直接输出
curl_setopt($ch, CURLOPT_HEADER, 0); // 不返回header部分
$data = curl_exec($ch);
curl_close($ch);
if ($data)
return $data;
else
return false;
}
2.测试
function test_url(){
$url = "http://211.151.59.44/s3c/samples/download/15009";
$data = $this->_curl_get($url);
$tmpFile = SAMPLE_TMP_PATH."a.sis";
$this->FileHelper->delFile($tmpFile);
$this->FileHelper->createFile($tmpFile,$data);
$this->_download("a.sis", $tmpFile);
}
3.下载
function _download($fileName, $downloadFile) {
$speed = 8.5; // 8,5 kb/s download rate limit
if (file_exists($downloadFile) && is_file($downloadFile)) {
header('Cache-control: private');
header('Content-Type: application/octet-stream');
header('Content-Length: ' . filesize($downloadFile));
header('Content-Disposition: filename=' . $fileName);
flush();
$fd = fopen($downloadFile, 'r');
while (!feof($fd)) {
echo fread($fd, round($speed * 1024));
flush();
sleep(1);
}
fclose($fd);
$this->log($downloadFile . 'file_exists', 'down');
} else {
$this->log($downloadFile . ': file_exists not');
return;
}
}