2016-09-07 220 views
0

我有一個可播放的.mp4文件,我使用此代碼使用PHP下載文件。使用PHP從網上下載MP4後無法播放MP4

網絡瀏覽器下載文件確定,但mp4文件下載後無法播放。

代碼有問題嗎?

<?php 
// place this code inside a php file and call it f.e. "download.php" 
$path = $_SERVER['DOCUMENT_ROOT'] . "/video/"; 
$fullPath = $path."test_video.mp4"; 

if ($fd = fopen ($fullPath, "r")) { 
    $fsize = filesize($fullPath); 
    $path_parts = pathinfo($fullPath); 
    header("Content-type: application/octet-stream"); 
    header("Content-Disposition: filename=\"".$path_parts["basename"]."\""); 
    header("Content-length: $fsize"); 
    header("Cache-control: private"); //use this to open files directly 
    while(!feof($fd)) { 
     $buffer = fread($fd, 2048); 
     echo $buffer; 
    } 
} 
fclose ($fd); 
exit; 
?> 
+0

請提供[mcve] – xenteros

回答

0

第一個header("Content-type: application/octet-stream");不需要下載文件。 Source

改變你的頭,

header('Content-type: video/mp4'); 
header('Content-type: video/mpeg'); 

這應該工作。

+0

如果我將其更改爲video/mp4,則會在網絡瀏覽器上顯示視頻,而不是下載視頻。 – jeon

+1

您可以強制瀏覽器在標題中下載。在這裏看到答案:http://stackoverflow.com/questions/21091766/mp4-download-causes-browser-to-play-file-instead-of-download –

0

我沒有評論的回覆。請檢查您的代碼是否有其他文件,例如文字或圖片。我認爲您也必須在那裏發現錯誤。