2011-03-31 45 views
1

文件我用這個代碼下載文件無法瀏覽網站,同時下載使用PHP

 
$file='test.mp3'; 
$download_rate = 50; //50 kb/s 

if(file_exists($file) && is_file($file)) 
{ 
    header('Cache-control: private'); 
    header('Content-Type: application/octet-stream'); 
    header('Content-Length: '.filesize($file)); 
    header('Content-Disposition: filename='.$file); 

    flush(); 
    $file = fopen($file, "r"); 

    while(!feof($file)) 
    { 
     // send the current file part to the browser 
     print fread($file, round($download_rate * 1024)); 
     // flush the content to the browser 
     flush(); 
     // sleep one second 
     sleep(1); 
    } 
    fclose($file); 
    } 
else { 
    echo 'File Not Found'; 
} 

但同時下載文件不能瀏覽該網站,直到下載完成。這發生在IE和火狐

任何答案?

+0

不確定但不要調用flush();定義標題後,只需在輸出文件數據時調用該標題。 – 2011-03-31 15:09:12

+0

這是什麼答案? – emaillenin 2011-08-21 10:14:52

回答

3

只有當我知道發生這種情況的時候,你有沒有被寫入的會話。 我在這裏看不到任何會話,所以我不確定是什麼導致它。 但是,大多數PHP下載文件腳本用於檢查登錄,所以我猜這是這種情況。 如果您確實有會話,請嘗試session_write_close();

+1

我有這樣的問題,這是我的問題的解決方案。謝謝! – CWSpear 2012-06-14 03:45:32

+0

很高興有幫助。 – frostymarvelous 2012-06-14 15:09:50