2011-03-31 18 views
1

我使用查詢生成文件如何強制用戶下載(另存爲對話框)剛剛生成的文件

SELECT * INTO OUTFILE " . "'c:/myfile.csv'" . " FIELDS TERMINATED BY ',' LINES TERMINATED BY '\n' FROM sample 

我的文件現在位於C:/myfile.csv中。我希望用戶下載該文件。我怎樣才能做到這一點。

Expers請幫忙。

+3

http://stackoverflow.com/search?q=[php]+force+download – 2011-03-31 17:39:14

+0

對不起,創建重複的朋友......真的很抱歉 – Rajasekar 2011-03-31 17:44:48

回答

2

,你可以:

  • 發送一個HTTP頭,以表示那種你發送的數據,並應被下載到文件中。
  • 然後,發送該文件的內容。


在PHP中,這樣的事情應該做的伎倆:

header('Content-Disposition: attachment; filename="my-file.csv"'); // The name which will be suggested to the user 
readfile('c:/myfile.csv'); // outputs the content of the file 
2

你需要設置Content-disposition: attachment在頭中。在PHP中,使用頭功能來實現這一點。

1
if([email protected]($fileString,'r')){ 
die("Cannot Open File!"); 
} else { 
header("Cache-Control: ");// leave blank to avoid IE errors 
header("Pragma: ");// leave blank to avoid IE errors 
header("Content-type: text/csv"); 
header("Content-Disposition: attachment; filename=\"".$fileName."\""); 
header("Content-length:".(string)(filesize($fileString))); 
sleep(1); 
fpassthru($fdl); 
} 
相關問題