2013-03-12 54 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); 

>

相关问题