PHP 从服务器上的目录下载 MP3 文件
PHP Download MP3 files from directory on server
我正在尝试将 MP3 文件下载到位于服务器上名为 "songs" 的目录中的用户计算机。我已经能够 运行 一个通过浏览器下载这些文件的脚本。但是,这些文件严格以带有 .mp3 扩展名的文本形式下载。我希望这些文件在从服务器下载后成为可播放的 mp3 文件。
这是我的 PHP 脚本。
<?php
$link = mysqli_connect("...","...","....","...") or die("Error ".mysqli_error($link));
if(mysqli_connect_errno())
{echo nl2br("Failed to connect to MySQL:". mysqli_connect_error() . "\n");}
else
{echo nl2br("Established Database Connection \n");}
//脚本当前下载在服务器上找到的所有歌曲的列表
$target = "songs/";
if ($handle = opendir($target)) {
while (false !== ($entry = readdir($handle))) {
if ($entry != "." && $entry != "..") {
echo $entry."'>".$entry."</a>\n";
}
}
closedir($handle);
}
$file = basename($_GET['file']);
$file = 'Songs_From_Server'.$file;
if(!$file){ // file does not exist
die('file not found');
} else {
header("Cache-Control: private");
header("Content-type: audio/mpeg3");
header("Content-Transfer-Encoding: binary");
header("Content-Disposition: attachment; filename=".basename($file));
readfile($file);
}
?>
这是我在 txt 文件中得到的结果示例。
已建立数据库连接
01 1983年他爱飞.mp3'>01 1983年他爱飞.mp3
首先,header()
应该在包含 echo
、print_r
、任何 html 的任何输出之前发送,在开始标记之前有一个空白 space (例如 <?php
)。参考
至 the manual
其次,如果你想用文件的内容响应浏览器,你的脚本不应该输出任何其他内容。除内容外的任何输出都将被视为内容的一部分。除非您将其作为多部分发送并且您的客户能够处理它。
举个例子
<?php
$fileName = $_GET['file'];
$path = '/directory/contains/mp3/';
$file = $path.$fileName;
if (!file_exists($file)) {
http_response_code(404);
die();
}
header("Cache-Control: private");
header("Content-type: audio/mpeg3");
header("Content-Transfer-Encoding: binary");
header("Content-Disposition: attachment; filename=".$fileName);
//So the browser can display the download progress
header("Content-Length: ".filesize($file));
readfile($file);
这对我有用
header("Content-type: application/mp3");
header("Content-Disposition: attachment; filename=".$s['song_name']);
header('Pragma: no-cache');
header('Expires: 0');
//So the browser can display the download progress
readfile('uploads/'.$s['song_mp3']);
exit;
我正在尝试将 MP3 文件下载到位于服务器上名为 "songs" 的目录中的用户计算机。我已经能够 运行 一个通过浏览器下载这些文件的脚本。但是,这些文件严格以带有 .mp3 扩展名的文本形式下载。我希望这些文件在从服务器下载后成为可播放的 mp3 文件。
这是我的 PHP 脚本。
<?php
$link = mysqli_connect("...","...","....","...") or die("Error ".mysqli_error($link));
if(mysqli_connect_errno())
{echo nl2br("Failed to connect to MySQL:". mysqli_connect_error() . "\n");}
else
{echo nl2br("Established Database Connection \n");}
//脚本当前下载在服务器上找到的所有歌曲的列表
$target = "songs/";
if ($handle = opendir($target)) {
while (false !== ($entry = readdir($handle))) {
if ($entry != "." && $entry != "..") {
echo $entry."'>".$entry."</a>\n";
}
}
closedir($handle);
}
$file = basename($_GET['file']);
$file = 'Songs_From_Server'.$file;
if(!$file){ // file does not exist
die('file not found');
} else {
header("Cache-Control: private");
header("Content-type: audio/mpeg3");
header("Content-Transfer-Encoding: binary");
header("Content-Disposition: attachment; filename=".basename($file));
readfile($file);
}
?>
这是我在 txt 文件中得到的结果示例。
已建立数据库连接
01 1983年他爱飞.mp3'>01 1983年他爱飞.mp3
首先,header()
应该在包含 echo
、print_r
、任何 html 的任何输出之前发送,在开始标记之前有一个空白 space (例如 <?php
)。参考
至 the manual
其次,如果你想用文件的内容响应浏览器,你的脚本不应该输出任何其他内容。除内容外的任何输出都将被视为内容的一部分。除非您将其作为多部分发送并且您的客户能够处理它。
举个例子
<?php
$fileName = $_GET['file'];
$path = '/directory/contains/mp3/';
$file = $path.$fileName;
if (!file_exists($file)) {
http_response_code(404);
die();
}
header("Cache-Control: private");
header("Content-type: audio/mpeg3");
header("Content-Transfer-Encoding: binary");
header("Content-Disposition: attachment; filename=".$fileName);
//So the browser can display the download progress
header("Content-Length: ".filesize($file));
readfile($file);
这对我有用
header("Content-type: application/mp3");
header("Content-Disposition: attachment; filename=".$s['song_name']);
header('Pragma: no-cache');
header('Expires: 0');
//So the browser can display the download progress
readfile('uploads/'.$s['song_mp3']);
exit;