Php文本框输出正在打印而不是下载
这个问题与另一个问题不同。那个问题是关于IF条件的。但这个问题不是下载文件,而是在下一页打印。请不要把这个问题和其他问题混淆起来 我写了一个脚本,下载文本框的输出。这个脚本在本地服务器上运行良好,它成功地下载了文件,但当我将脚本放到live server上时,下载文件不起作用。单击Php文本框输出正在打印而不是下载,php,download,Php,Download,这个问题与另一个问题不同。那个问题是关于IF条件的。但这个问题不是下载文件,而是在下一页打印。请不要把这个问题和其他问题混淆起来 我写了一个脚本,下载文本框的输出。这个脚本在本地服务器上运行良好,它成功地下载了文件,但当我将脚本放到live server上时,下载文件不起作用。单击下载输出按钮时,页面的输出显示在下一页,而不是下载文件 请参见此处的实时脚本: 以下是脚本代码: <?php error_reporting(0); $mytext = ""; $tx
下载输出
按钮时,页面的输出显示在下一页,而不是下载文件
请参见此处的实时脚本:
以下是脚本代码:
<?php
error_reporting(0);
$mytext = "";
$txt = preg_replace('/\n+/', "\n", trim($_POST['inputtext']));
$text = explode("\n", $txt);
$output = array();
if(isset($_POST["submit"]) || isset($_POST["download"]) ) {
for($i=0;$i<count($text);$i++) {
$output[] .= trim($text[$i]) . ' AAAAA'.PHP_EOL;
}
}
if(isset($_POST["download"]) ) {
ob_clean();
$filename = "file-name-" .date('mdY-His'). '.txt';
$handle = fopen('php://output', "w");
fwrite($handle, implode($output));
fclose($handle);
header('Content-Description: File Transfer');
header("Content-type: application/force-download");
header("Content-Type: application/octet-stream charset=utf-8");
header('Content-Disposition: attachment; filename="'.basename($filename).'"');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Expires: 0');
header('Pragma: public');
header('Content-Transfer-Encoding: binary');
header('Content-Length: ' . strlen(implode($output)));
readfile($filename);
exit();
}
?>
<form method="POST" action="test.php">
<textarea name="inputtext" rows="10" cols="100" placeholder="Enter Any Text!" required><?php if(!empty($_POST["inputtext"])) { echo $_POST["inputtext"]; } ?></textarea>
<br><br>
<input type="submit" name="submit" value="Do it!">
<br><br>
<p>Output goes here. </p>
<textarea name="oputputtext" rows="10" cols="100" ><?php echo implode($output);?></textarea>
<input type="submit" name="download" value="Download Output">
</form>
这对我很有用:
$handle = fopen("file.txt", "w");
fwrite($handle, "text1.....");
fclose($handle);
header('Content-Type: application/octet-stream');
//You dont need to enclose the filename value in quotes
header('Content-Disposition: attachment; filename='.basename('file.txt'));
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize('file.txt'));
readfile('file.txt');
exit;
井错误报告(0)代码>这对你没有帮助;你需要将其设置为捕获和显示。你不是已经发布了吗?并收到了一份可能重复的回答“不”,这是不重复的。这是关于下载按钮的条件。。但这是关于下载文件。这是有效的,但如果我添加了标题,如果它只有这个html标记,那么它不会创建下载文件,而是像上面我给出的链接一样打印。。。