2010-04-26 53 views
1

或者用戶可能下載文件而不保存在服務器上? 我正在從數據庫獲取數據,並且想要保存它們.doc(MS Word)文件。下載文件,而不保存在服務器中

if(isset($_POST['save'])) 
{ 
$file = "new_file2.doc"; 
$stringData = "Text text text text...."; //This data put in new Word file 

$fh = fopen($file, 'w');  
fwrite($fh, $stringData); 
fclose($fh); 

header('Content-Description: File Transfer'); 
header('Content-type: application/msword'); 
header('Content-Disposition: attachment; filename="'.$file.'"'); 
header('Content-Transfer-Encoding: binary'); 
header('Expires: 0'); 
header('Cache-Control: must-revalidate, post-check=0, pre-check=0'); 
header('Pragma: public'); 
header('Content-Length: ' . filesize($file)); 
ob_clean(); 
flush(); 
readfile($file);  
unlink($file); 
exit; 

}

應該如何代碼看起來像沒有這個: 「$跳頻=的fopen($文件, 'W');
的fwrite($跳頻,$ StringData是); FCLOSE( $ FH);」 和這個「取消鏈接($文件);」

我希望能理解我需要什麼 這裏

回答

5

你只需要輸入代碼的輸出頭,然後將內容:

header(...); 

echo $stringData; 

無需任何文件的播放。

0
header("Content-type: application/octet-stream");  
header("Content-Disposition: attachment; 
filename=\"$_fileName\""); 
ob_clean(); 
readfile($_filePath); 
相關問題