2011-07-17 55 views
0

我想製作一個PHP腳本來下載一個AVI文件。該文件在我的服務器上,我想將其發送給用戶。我做了下面的腳本,但是當我運行它時,我只會得到一個0 KB的大AVI文件。PHP頭附加AVI文件

誰能告訴我我做錯了什麼?

$file_path = "downloads/test.avi"; 

// Get filename 
$filename = explode("/", $file_path); 
$filename = $filename[count($filename)-1]; 

if(file_exists($file_path)) { 
    $file_extension = strtolower(substr(strrchr($file_path, "."), 1)); 

    // This will set the Content-Type to the appropriate setting for the file 
    switch($file_extension) { 
     case "pdf": 
      $ctype = "application/pdf"; 
      break; 
     case "exe": 
      $ctype = "application/octet-stream"; 
      break; 
     case "zip": 
      $ctype = "application/zip"; 
      break; 
     case "doc": 
      $ctype = "application/msword"; 
      break; 
     case "xls": 
      $ctype = "application/vnd.ms-excel"; 
      break; 
     case "ppt": 
      $ctype = "application/vnd.ms-powerpoint"; 
      break; 
     case "gif": 
      $ctype = "image/gif"; 
      break; 
     case "png": 
      $ctype = "image/png"; 
      break; 
     case "jpeg": 
      $ctype = "image/jpg"; 
      break; 
     case "jpg": 
      $ctype = "image/jpg"; 
      break; 
     case "mp3": 
      $ctype = "audio/mpeg"; 
      break; 
     case "wav": 
      $ctype = "audio/x-wav"; 
      break; 
     case "mpeg": 
      $ctype = "video/mpeg"; 
      break; 
     case "mpg": 
      $ctype = "video/mpeg"; 
      break; 
     case "mpe": 
      $ctype = "video/mpeg"; 
      break; 
     case "mov": 
      $ctype = "video/quicktime"; 
      break; 
     case "avi": 
      $ctype = "video/x-msvideo"; 
      break; 
     case "src": 
      $ctype = "plain/text"; 
      break; 
     default: 
      $ctype = "application/force-download"; 
    } 

    $filesize = filesize($file_path); 

    // Set content type 
    header("Content-type: " . $ctype); 

    // Download file 
    header("Content-Disposition: attachment; filename=\"" . $filename . "\""); 

    // Set size of file 
    header("Content-Length: " . $filesize); 

    readfile($file_path); 

這是我在Firefox從的LiveHTTPHeaders得到(出於某種原因Content-Length爲零):

HTTP/1.1 200 OK 
Date: Sun, 17 Jul 2011 14:34:24 GMT 
Server: Apache/2.2.6 mod_auth_kerb/5.3 PHP/5.2.17 mod_fcgid/2.3.5 
X-Powered-By: PHP/5.2.17 
Expires: Thu, 19 Nov 1981 08:52:00 GMT 
Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0 
Pragma: no-cache 
Content-Disposition: attachment; filename="test.avi" 
Content-Length: 0 
Connection: close 
Content-Type: video/x-msvideo 

你可能想嘗試在網站上騰出自己在http://snuzzer.dk/nas/client.php

+2

請注意:使用帶鍵的數組作爲文件擴展名和值作爲它們的MIME類型。將減少你的代碼,並且可能更容易編輯 – hakre

+0

readfile()返回讀取的字節數,你能檢查它是否真的讀取文件嗎?可能是權限問題。 – sanmai

回答

2

添加此行readfile()

header("Content-Length: " . filesize($file_path)); 
+0

正如我剛剛寫給@alex,結果是完全相同有或沒有'Content-Length' – simonbs

+0

我剛剛嘗試在Firefox中使用LiveHTTPHeaders,由於某些原因,當我使用'readfile()'時,Firefox不會打開頁面它和LiveHTTPHeaders說'內容長度'是0.請檢查我原來的帖子。 – simonbs

0

T認爲它,我認爲這將工作!

header ("Pragma: public"); 
header ("Expires: 0"); 
header ("Cache-Control: must-revalidate, post-check=0, pre-check=0"); 
header ("Content-Type: application/force-download"); 
header ("Content-Type: application/octet-stream"); 
header ("Content-Type: application/download"); 
header ("Content-Disposition: attachment; filename=\"" . $file_path . "\";"); 
header ("Content-Transfer-Encoding: binary "); 
header ("Content-type: " . $ctype); 

$chunksize = 1 * (1024 * 1024); // how many bytes per chunk 
if ($size > $chunksize) { 
    $handle = fopen($file_path, 'rb'); 
    $buffer = ''; 
    while (!feof($handle)) { 
    $buffer = fread($handle, $chunksize); 
    echo $buffer; 
    ob_flush(); 
    flush(); 
    } 
    fclose($handle); 
    exit();