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 stm32流水灯+呼吸灯+外部中断按键
  • ¥15 将二维数组,按照假设的规定,如0/1/0 == "4",把对应列位置写成一个字符并打印输出该字符
  • ¥15 NX MCD仿真与博途通讯不了啥情况
  • ¥15 win11家庭中文版安装docker遇到Hyper-V启用失败解决办法整理
  • ¥15 gradio的web端页面格式不对的问题
  • ¥15 求大家看看Nonce如何配置
  • ¥15 Matlab怎么求解含参的二重积分?
  • ¥15 苹果手机突然连不上wifi了?
  • ¥15 cgictest.cgi文件无法访问
  • ¥20 删除和修改功能无法调用