https://www.nodeseek.com/post-223314-1
<?php
if (isset($_GET['url'])) {
$url = filter_var($_GET['url'], FILTER_VALIDATE_URL); // 验证输入的 URL
if (!$url) {
die("无效的 URL");
}
// 获取文件名(从 URL 提取)
$fileName = basename(parse_url($url, PHP_URL_PATH));
// 设置头信息,告诉浏览器这是一个下载请求
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename="' . $fileName . '"');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($url));
// 使用 fopen/fpassthru 直接将远程文件流转发给用户
$remoteFile = fopen($url, 'rb');
if ($remoteFile) {
while (!feof($remoteFile)) {
echo fread($remoteFile, 8192); // 按块读取数据
flush(); // 确保数据实时发送到用户
}
fclose($remoteFile);
} else {
die("无法读取远程文件");
}
exit;
}
?>