2013-08-07 40 views
2

我工作的一個項目,我需要爲影片下載功能,如文件下載,現在我的下載代碼如下關於PHP

header("Cache-Control: public"); 
header("Content-Description: File Transfer"); 
header("Content-Disposition: attachment; filename=".$fileName); 
header("Content-Type: video/mp4"); 
header("Content-Type: video/webm"); 
header("Content-Type: video/flv"); 
header("Content-Transfer-Encoding: binary"); 
readfile($downloadUrl); 

上面的代碼工作正常。但事情是我的文件格式可以是任何類型的例如。 .flv,.mp4,.3gp。

所以,我的問題是上面的代碼需要通過每種類型的Content-Type。是否有可以通過的支持所有文件格式的通用Content-Type。

+1

我不認爲你必須在所有指定內容類型。這只是瀏覽器如何處理它的暗示。無論如何,你正在下載文件。如果你必須使用內容類型,讓它成爲'應用程序'。 – Antoniossss

回答

2

是:application/octet-stream。如果你對打開文件不感興趣,這個MIME類型告訴瀏覽器該文件是一些未指定的類型,只能保存到磁盤。

+0

謝謝約翰。它像一個魅力。 –

3

在通常情況下,你可以只使用

header('Content-type: application/octet-stream') 

發送特定內容類型只是幫助客戶瞭解哪些應用程序應該被用來打開傳入文件。由於您正在發送內容處置附件,因此無需打開文件。

+0

謝謝尤金......你的答案也可以。我有upvoted –

0

這可能會幫助你

下載腳本與恢復選項

<?php 
// get the file request, throw error if nothing supplied 

// hide notices 
@ini_set('error_reporting', E_ALL & ~ E_NOTICE); 

//- turn off compression on the server 
@apache_setenv('no-gzip', 1); 
@ini_set('zlib.output_compression', 'Off'); 

if(!isset($_REQUEST['file']) || empty($_REQUEST['file'])) 
{ 
    header("HTTP/1.0 400 Bad Request"); 
    exit; 
} 

// sanitize the file request, keep just the name and extension 
// also, replaces the file location with a preset one ('./myfiles/' in this example) 
$file_path = $_REQUEST['file']; 
$path_parts = pathinfo($file_path); 
$file_name = $path_parts['basename']; 
$file_ext = $path_parts['extension']; 
$file_path = './myfiles/' . $file_name; 

// allow a file to be streamed instead of sent as an attachment 
$is_attachment = isset($_REQUEST['stream']) ? false : true; 

// make sure the file exists 
if (is_file($file_path)) 
{ 
    $file_size = filesize($file_path); 
    $file = @fopen($file_path,"rb"); 
    if ($file) 
    { 
     // set the headers, prevent caching 
     header("Pragma: public"); 
     header("Expires: -1"); 
     header("Cache-Control: public, must-revalidate, post-check=0, pre-check=0"); 
     header("Content-Disposition: attachment; filename=\"$file_name\""); 

     // set appropriate headers for attachment or streamed file 
     if ($is_attachment) 
       header("Content-Disposition: attachment; filename=\"$file_name\""); 
     else 
       header('Content-Disposition: inline;'); 

     // set the mime type based on extension, add yours if needed. 
     $ctype_default = "application/octet-stream"; 
     $content_types = array(
       "exe" => "application/octet-stream", 
       "zip" => "application/zip", 
       "mp3" => "audio/mpeg", 
       "mpg" => "video/mpeg", 
       "avi" => "video/x-msvideo", 
     ); 
     $ctype = isset($content_types[$file_ext]) ? $content_types[$file_ext] : $ctype_default; 
     header("Content-Type: " . $ctype); 

     //check if http_range is sent by browser (or download manager) 
     if(isset($_SERVER['HTTP_RANGE'])) 
     { 
      list($size_unit, $range_orig) = explode('=', $_SERVER['HTTP_RANGE'], 2); 
      if ($size_unit == 'bytes') 
      { 
       //multiple ranges could be specified at the same time, but for simplicity only serve the first range 
       //http://tools.ietf.org/id/draft-ietf-http-range-retrieval-00.txt 
       list($range, $extra_ranges) = explode(',', $range_orig, 2); 
      } 
      else 
      { 
       $range = ''; 
       header('HTTP/1.1 416 Requested Range Not Satisfiable'); 
       exit; 
      } 
     } 
     else 
     { 
      $range = ''; 
     } 

     //figure out download piece from range (if set) 
     list($seek_start, $seek_end) = explode('-', $range, 2); 

     //set start and end based on range (if set), else set defaults 
     //also check for invalid ranges. 
     $seek_end = (empty($seek_end)) ? ($file_size - 1) : min(abs(intval($seek_end)),($file_size - 1)); 
     $seek_start = (empty($seek_start) || $seek_end < abs(intval($seek_start))) ? 0 : max(abs(intval($seek_start)),0); 

     //Only send partial content header if downloading a piece of the file (IE workaround) 
     if ($seek_start > 0 || $seek_end < ($file_size - 1)) 
     { 
      header('HTTP/1.1 206 Partial Content'); 
      header('Content-Range: bytes '.$seek_start.'-'.$seek_end.'/'.$file_size); 
      header('Content-Length: '.($seek_end - $seek_start + 1)); 
     } 
     else 
      header("Content-Length: $file_size"); 

     header('Accept-Ranges: bytes'); 

     set_time_limit(0); 
     fseek($file, $seek_start); 

     while(!feof($file)) 
     { 
      print(@fread($file, 1024*8)); 
      ob_flush(); 
      flush(); 
      if (connection_status()!=0) 
      { 
       @fclose($file); 
       exit; 
      }   
     } 

     // file save was a success 
     @fclose($file); 
     exit; 
    } 
    else 
    { 
     // file couldn't be opened 
     header("HTTP/1.0 500 Internal Server Error"); 
     exit; 
    } 
} 
else 
{ 
    // file does not exist 
    header("HTTP/1.0 404 Not Found"); 
    exit; 
} 
?> 
+0

你的腳本很酷,我看到這裏的工作,但是你知道嗎,你的服務器上的任何文件甚至網絡上可用的文件都可以通過它下載?巨大的安全漏洞和東西。我的網絡服務器被這樣的腳本弄壞了。 – Joshua

0

務必:

header("Content-Type: ".mime_content_type($downloadUrl));