下载PHP脚本

时间:2013-07-07 08:32:12

标签: php html

在我的网站中,我希望用户在单击按钮时下载音乐文件。我遇到的问题是,当单击按钮时,下载开始,但总共只下载了506 KB,总共4 MB,打开时显示不支持的格式。我使用这个带有html格式的php下载脚本进行下载:

HTML表格

<FORM ACTION="download.php" METHOD=POST>
  <INPUT TYPE="hidden" NAME="music"    VALUE= "music/<? print $file; ?>"  >
  <INPUT TYPE="SUBMIT" NAME="download"  VALUE="Download">
</FORM>

PHP DOWNLOAD SCRIPT

<?php
$FileName = $_POST["music"];
$FilePath = "music";
$size = filesize($FilePath . $FileName);
header("Content-Type: application/force-download; name=\"". $FileName ."\"");
header("Content-Transfer-Encoding: binary");
header("Content-Length: ". $size ."");
header("Content-Disposition: attachment; filename=\"". $FileName ."\"");
header("Expires: 0");
header("Cache-Control: private");
header("Pragma: private");
echo (readfile($FilePath . $FileName));
?>

你能指点我这个问题吗?

1 个答案:

答案 0 :(得分:1)

您的代码正在以下位置查找文件:

musicmusic/file.mp3

假设您的文件实际存储在music/file.mp3之类,而变量$file包含file.mp3之类的内容,则您想要替换:

<INPUT TYPE="hidden" NAME="music" VALUE= "music/<? print $file; ?>">

使用:

<INPUT TYPE="hidden" NAME="music" VALUE= "<? print $file; ?>">

然后替换它:

$FilePath = "music";

使用:

$FilePath = "music/";

(或完全删除$FilePath

此外,我建议您在让任何人从您的服务器下载任何文件之前,先检查$_POST["music"]不包含..或以/开头。