我的网站上有下表:
<table>
<tr>
<td>Song Name: Far-Sighted</td>
<td>
<a href=http://michaelpitluk.com/audio/far_sighted.mp3>Download</a>
</td>
</tr>
</table>
我的目标是当人们点击“下载”时,他们的浏览器将被迫下载 mp3。
我已经研究了执行此操作所需的 php 脚本,但我不明白如何将其连接到歌曲的链接。
我是否在我的 php 目录下创建一个新文件并将其命名为 download.php,插入脚本,并以某种方式让它指向歌曲?
这是我试图链接到我的歌曲的 php 脚本:
<?php
// Fetch the file info.
$filePath = '/path/to/file/on/disk.jpg';
if(file_exists($filePath)) {
$fileName = basename($filePath);
$fileSize = filesize($filePath);
// Output headers.
header("Cache-Control: private");
header("Content-Type: application/stream");
header("Content-Length: ".$fileSize);
header("Content-Disposition: attachment; filename=".$fileName);
// Output file.
readfile ($filePath);
exit();
}
else {
die('The provided file path is not valid.');
}
?>
(来自:如何在 PHP 中强制文件下载)