PHP:Header 强制下载损坏(MP3 文件)
PHP: Header Force Download Corrupt (MP3 File)
我正在创建一个可让您下载歌曲的简单网站。但是,我的脚本会下载 mp3 文件,但当我尝试打开它时,它不知何故已损坏。
不仅如此,文件大小也明显减少。
代码:
function send_download($file)
{
$basename = basename($file);
$length = sprintf("%u", filesize($file));
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename="' . $basename . '"');
header('Content-Transfer-Encoding: binary');
header('Connection: Keep-Alive');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
header('Content-Length: ' . $length);
set_time_limit(0);
readfile($file);
}
我在这里和 Google 搜索了几个小时。有帮助吗?
提前致谢。
似乎这还不够,这是我的整个 php 文件:
<?php
error_reporting(0);
echo "<style>
body {
font-family: Verdana;
background-color:#D9DDDB;
color:black;
}
</style>
";
echo "<br><br><br><center><h1>Your Download is Starting...</h1></center><br><br><br><center><p>Copyright© 2015 <font color='blue'>MusicProductionZ</font></p></center>";
if(isset($_GET['access_token'])) {
if($_GET['access_token'] == '3486784401') {
if(isset($_GET['file_name'])) {
// Add More Here
switch($_GET['file_name']) {
case 'fettywap_trapqueen':
$filename = "/files/fettywap_trapqueen/Fetty Wap - Trap Queen.mp3";
send_download($filename);
exit;
break;
default:
break;
exit;
}
}
}
}
function send_download($file)
{
$basename = basename($file);
$length = sprintf("%u", filesize($file));
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename="' . $basename . '"');
header('Content-Transfer-Encoding: binary');
header('Connection: Keep-Alive');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
header('Content-Length: ' . $length);
set_time_limit(0);
readfile($file);
}
如果您使用的是 Apache2,我强烈建议您使用 mod_xsendfile
如果 运行 Ubuntu 或类似的你可以安装它:
sudo apt-get install libapache2-mod-xsendfile
sudo a2enmod xsendfile
然后在您的 VirtualHost
中打开 XSendFile 并提供批准的路径:
<VirtualHost *:80>
......
<Directory "/var/www/music">
Options FollowSymLinks
AllowOverride All
XSendFile On
XSendFilePath /home/me/Music
XSendFilePath /media/me/portable/Music
</Directory>
然后要真正发送文件,您需要做的就是:
if(isset($_GET['f'])){
if(is_dir($path.'/'.$_GET['f'])){
display_files($path,$_GET['f']);
}else{
//send selected file
header('X-SendFile: ' . $path.'/'.$_GET['f']);
header("Content-type: audio/mpeg");
header('Content-disposition: attachment; filename="'.basename($_GET['f'].'"'));
}
}else{
display_files($path);
}
无需担心 echo
在发送文件或重定向到空下载页面之前输入文本,因为这里我有一个完整的目录列表,并且在我发送文件之前显示了一些没有损坏.
我正在创建一个可让您下载歌曲的简单网站。但是,我的脚本会下载 mp3 文件,但当我尝试打开它时,它不知何故已损坏。 不仅如此,文件大小也明显减少。
代码:
function send_download($file)
{
$basename = basename($file);
$length = sprintf("%u", filesize($file));
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename="' . $basename . '"');
header('Content-Transfer-Encoding: binary');
header('Connection: Keep-Alive');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
header('Content-Length: ' . $length);
set_time_limit(0);
readfile($file);
}
我在这里和 Google 搜索了几个小时。有帮助吗?
提前致谢。
似乎这还不够,这是我的整个 php 文件:
<?php
error_reporting(0);
echo "<style>
body {
font-family: Verdana;
background-color:#D9DDDB;
color:black;
}
</style>
";
echo "<br><br><br><center><h1>Your Download is Starting...</h1></center><br><br><br><center><p>Copyright© 2015 <font color='blue'>MusicProductionZ</font></p></center>";
if(isset($_GET['access_token'])) {
if($_GET['access_token'] == '3486784401') {
if(isset($_GET['file_name'])) {
// Add More Here
switch($_GET['file_name']) {
case 'fettywap_trapqueen':
$filename = "/files/fettywap_trapqueen/Fetty Wap - Trap Queen.mp3";
send_download($filename);
exit;
break;
default:
break;
exit;
}
}
}
}
function send_download($file)
{
$basename = basename($file);
$length = sprintf("%u", filesize($file));
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename="' . $basename . '"');
header('Content-Transfer-Encoding: binary');
header('Connection: Keep-Alive');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
header('Content-Length: ' . $length);
set_time_limit(0);
readfile($file);
}
如果您使用的是 Apache2,我强烈建议您使用 mod_xsendfile
如果 运行 Ubuntu 或类似的你可以安装它:
sudo apt-get install libapache2-mod-xsendfile
sudo a2enmod xsendfile
然后在您的 VirtualHost
中打开 XSendFile 并提供批准的路径:
<VirtualHost *:80>
......
<Directory "/var/www/music">
Options FollowSymLinks
AllowOverride All
XSendFile On
XSendFilePath /home/me/Music
XSendFilePath /media/me/portable/Music
</Directory>
然后要真正发送文件,您需要做的就是:
if(isset($_GET['f'])){
if(is_dir($path.'/'.$_GET['f'])){
display_files($path,$_GET['f']);
}else{
//send selected file
header('X-SendFile: ' . $path.'/'.$_GET['f']);
header("Content-type: audio/mpeg");
header('Content-disposition: attachment; filename="'.basename($_GET['f'].'"'));
}
}else{
display_files($path);
}
无需担心 echo
在发送文件或重定向到空下载页面之前输入文本,因为这里我有一个完整的目录列表,并且在我发送文件之前显示了一些没有损坏.