Php 所有Mp3文件仅以3kb下载且已损坏
这段代码强制我在Chrome和Mozilla上下载。但问题是所有文件大小都是3kb。为什么?大小不同,如3MB、4MB、3.5MB,下载文件只是一个损坏的3kb。。。请帮忙 注意:如果使用标题重定向,它将在Chrome中正常下载,或在其他浏览器中打开,并在不下载的情况下开始播放Php 所有Mp3文件仅以3kb下载且已损坏,php,Php,这段代码强制我在Chrome和Mozilla上下载。但问题是所有文件大小都是3kb。为什么?大小不同,如3MB、4MB、3.5MB,下载文件只是一个损坏的3kb。。。请帮忙 注意:如果使用标题重定向,它将在Chrome中正常下载,或在其他浏览器中打开,并在不下载的情况下开始播放 <?php //Bring in My functions require_once ('../inc/functions.php'); //Bring in my header r
<?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');
?>
在执行readfile时,您已经将包含的文件和一些HTML写入了输出缓冲区。这将导致媒体文件损坏。在输出任何HTML内容之前,您可能需要放置readfile并退出(或die())
而站点URL对$filePath不起作用。您可能需要通过预结束找到正确的文件路径
...dirname(dirname(__file__))...
谢谢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();
}
}
?>
使用完整的本地路径/path/to/mp3
而不是$filePath=SITE\u URL.“music/”$file
中的SITE\u URL
,这并没有解决问题。我已经试过101%确定了SITE\u URL.“music/”$file
和/path/to/my/mp3
仍然是相同的输出在代码编辑器中打开mp3文件。错误信息应该在你的3kb mp3文件中。我在记事本中打开了mp3文件,里面只有文件路径http://localhost/du/my-mp3 mp3文件
,我再次将其复制到我的url,它下载了适用于meThanks Vishva8kumara的正确文件。我已经删除了所有的输出,它下载的大小为零字节。下面是新的代码块注释的标题部分现在,所以你宁愿看到mp3文件的内容在浏览器上。这将更容易调试代码,因此您可以取消对标题的注释。然后,代替readfile,回显$filePath并查看是否获得正确的文件路径。如果我回显$filePath,则不会发生任何事情。相反,它仍然下载一个3kbHmm的损坏文件。。。我将文件路径更改为$filePath=“C:\wamp\www\du\music\\”$file代码>它下载了正确的文件,但是我想把它删除到服务器上的时候呢?我该怎么办?