2017-05-04 105 views
0

此代碼強制在Chrome和Mozilla上爲我下載。但問題是所有文件大小都是3kb。爲什麼?大小是不同的像3MB,4MB,3.5MB和下載文件只是一個腐敗的3kb ...請幫助。所有Mp3文件僅以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'); 
?> 
+1

使用完整的本地路徑'/路徑//mp3'的,而不是''中的文件路徑$ = SITE_URL SITE_URL'。「音樂/".$ file' –

+0

這並沒有解決問題。我已經嘗試過101%的確定。 'SITE_URL。「music /」.$ file'和'/ path/to/my/mp3'仍然是相同的輸出 –

+0

在代碼編輯器中打開mp3文件。錯誤信息應該在你的3kb mp3文件中。 – vishva8kumara

回答

0

當您執行讀取文件時,您已經寫入了包含文件和一些HTML到輸出緩衝區。這將導致媒體文件損壞。您可能希望在發送任何HTML內容之前先放入readfile並退出(或die())。

而SITE_URL不適用於$ filePath。您可能需要通過預先

...dirname(dirname(__file__))... 
+0

謝謝** Vishva8kumara **。我已經刪除了所有輸出,並且它的大小爲零字節。以下是新代碼 –

+0

現在將塊頭註釋爲頭部分,以便您更願意在瀏覽器中看到mp3文件內容。這將更容易調試代碼,因此您可以取消註釋頭。 – vishva8kumara

+0

然後,代替readfile,回顯$ filePath並查看是否獲得了正確的文件路徑。 – vishva8kumara

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(); 
      } 
     } 
?>