此代码强制下载适用于Chrome和Mozilla。但问题是所有文件大小都是3kb。为什么?大小不同,如3MB,4MB,3.5MB,下载文件只是一个损坏的3kb ......请帮忙。
注意:如果使用标题重定向,则可以在Chrome中正常下载或在其他浏览器中打开并开始播放而无需下载。
<?php
//Bring in My functions
require_once ('../inc/functions.php');
//Bring in my header
require_once ('../inc/header.php');
?>
<!-- Page body starts -->
<div class="container">
<div class="col-md-9 contentArea">
<?php
//Get File, Check For Existence and Download
//Get File, Check For Existence and Download
if(isset($_GET['file'])){
$file = sanitizeMySQL($con, $_GET['file']);
$checkFile = "SELECT * FROM files WHERE f_song = '$file' AND f_del='0'";
$checkFileRun = mysqli_query($con, $checkFile);
$fileName = mysqli_fetch_assoc($checkFileRun);
//$song = $fileName['f_song'];
if($fileName['f_song']==null || $fileName['f_song'] == ""){
echo '<h4 class="text-center"><i class="fa fa-times-circle fa-3x red"></i><br />Sorry! This is does not Exist on this Server or has been moved</h4>';
}else{
$updateDownload = "UPDATE b_files SET f_count={$fileName['f_count']}+1 WHERE f_song ='$file' AND f_del='0'";
$updateDownloadRun = mysqli_query($con, $updateDownload);
$filePath = SITE_URL."music/".$file;
$fileSize = filesize($filePath);
header("Cache-Control: public");
header("Content-Description: File Transfer");
header("Content-Length: ".$fileSize);
header("Content-Disposition: attachment; filename=".$file);
header("Content-Type: audio/mpeg, audio/x-mpeg, audio/x-mpeg-3, audio/mpeg3");
header("Content-Transfer-Encoding: binary");
//Read the file
readfile($filePath);
//header("Location:".SITE_URL.'music/'.urldecode($fileName['file_song_name']));
exit();
}
}elseif(!isset($_GET['file'])){
echo '<h4 class="text-center"><i class="fa fa-times-circle fa-3x red"></i><br />Sorry! File Not Found</h4>';
}
//File Download Ends
?>
</div>
<!-- Call In The SideBar -->
<?php require_once '../inc/sidebar.php'; ?>
</div>
<?php
require_once ('../inc/footer.php');
?>
答案 0 :(得分:0)
执行readfile时,您已经将包含的文件和一些HTML写入输出缓冲区。这将导致媒体文件损坏。你可能想在放弃任何HTML内容之前放入readfile并退出(或die())。
SITE_URL对$ filePath不起作用。您可能需要通过预先添加
来找到正确的文件路径...dirname(dirname(__file__))...
答案 1 :(得分:0)
感谢 Vishva8kumara 。我删除了所有输出,它下载大小为零字节,文件已损坏。以下是新代码
<?php
//Bring in My functions
require_once ('../inc/functions.php');
//Get File, Check For Existence and Download
//Get File, Check For Existence and Download
if(isset($_GET['file'])){
$file = sanitizeMySQL($con, $_GET['file']);
$checkFile = "SELECT * FROM files WHERE f_song = '$file' AND f_del='0'";
$checkFileRun = mysqli_query($con, $checkFile);
$fileName = mysqli_fetch_assoc($checkFileRun);
//$song = $fileName['f_song'];
if($fileName['f_song']==null || $fileName['f_song'] == ""){
echo '<h4 class="text-center"><i class="fa fa-times-circle fa-3x red"></i><br />Sorry! This is does not Exist on this Server or has been moved</h4>';
}else{
$updateDownload = "UPDATE b_files SET f_count={$fileName['f_count']}+1 WHERE f_song ='$file' AND f_del='0'";
$updateDownloadRun = mysqli_query($con, $updateDownload);
$filePath = "http://localhost/du/music/".$file;
$fileSize = filesize($filePath);
header("Cache-Control: public");
header("Content-Description: File Transfer");
header("Content-Length: ".$fileSize);
header("Content-Disposition: attachment; filename=".$file);
header("Content-Type: audio/mpeg, audio/x-mpeg, audio/x-mpeg-3, audio/mpeg3");
header("Content-Transfer-Encoding: binary");
//Read the file
readfile($filePath);
//header("Location:".SITE_URL.'music/'.urldecode($fileName['file_song_name']));
exit();
}
}
?>