2013-03-12 31 views
1

我有這種形式,您在文本區域中輸入文本,並提交PHP生成一個文本文件,應該包含文本輸入的文本區域。 但問題出在我寫的代碼,文本文件沒有下載,但沒有文本。 因此,我將文本區域更改爲輸入字段與文本類型,並返回文本文件中的數據,但沒有換行符。這裏是我的代碼PHP txt下載不寫文本區域內容

<html><body>  
<form action="download.php" method="post"> 
<textarea id="output" name="output"></textarea> 
<input type="hidden" name="op" type="hidden" id="fakeop" > 
<input type="submit"> 
</body></html> 

和PHP文件的內容是

<?php 
$filename = 'random.txt'; 
$somecontent = $_REQUEST["output"]; 
!$handle = fopen($filename, 'w+'); 
fwrite($handle, $somecontent); 
fclose($handle); 

header("Cache-Control: public"); 
header("Content-Description: File Transfer"); 
header("Content-Length: ". filesize("$filename").";"); 
header("Content-Disposition: attachment; filename=$filename"); 
header("Content-Type: application/octet-stream; "); 
header("Content-Transfer-Encoding: binary"); 

readfile($filename); 

?> 

,如果我改變將$ somecontent = $ _REQUEST [ 「OP」]; 它確實返回帶有內容的文本文件。但爲什麼不在其產出時。?

+2

回聲將$ somecontent = $ _REQUEST [ 「輸出」];檢查你是否收到的文字。 – Samy 2013-03-12 09:13:36

+1

你是否在代碼的任何地方關閉表單標籤?也可以嘗試在method =「post」之後的form標籤中添加enctype =「multipart/form-data」。 – Fredd 2013-03-12 09:14:49

+2

爲什麼不使用'$ _POST'? – Jon 2013-03-12 09:16:12

回答

0

您好,請與下面的代碼測試PHP頁面

<?php 
$filename = 'random.txt'; 
$somecontent = $_POST["output"]; 
!$handle = fopen($filename, 'w+'); 
fwrite($handle, $somecontent); 
fclose($handle); 

header("Cache-Control: public"); 
header("Content-Description: File Transfer"); 
header("Content-Length: ". filesize("$filename").";"); 
header("Content-Disposition: attachment; filename=$filename"); 
header("Content-Type: application/octet-stream; "); 
header("Content-Transfer-Encoding: binary"); 

readfile($filename); 

>

相關問題