dongtigai3875 2017-05-04 17:07
浏览 65
已采纳

所有Mp3文件仅以3kb下载并损坏

This code force downloads fine for me on both Chrome and Mozilla. But the issue is that all file size are 3kb. Why? The sizes are different like 3MB, 4MB, 3.5MB and the download file is just a corrupt 3kb... Kindly help please.

Note: If a use a header redirection, it downloads fine in Chrome or opens in other browsers and start playing without downloading.

<?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');
?>
  • 写回答

2条回答 默认 最新

  • douba6361 2017-05-04 18:23
    关注

    When you do the readfile, you have already written included files and some HTML to the output buffer. This will result in a corrupted media file. You may want to put the readfile and exit (or die()) before outing any HTML content.

    And SITE_URL wouldn't work for $filePath. You may need to find the correct file path by prepending

    ...dirname(dirname(__file__))...
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 关于大棚监测的pcb板设计
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)