douzhang1364 2017-05-18 11:51 采纳率: 0%
浏览 25
已采纳

Php文本框输出是打印而不是下载

This problem is different of the other question problem. That problem was about IF condition. But this problem, instead of downloading file, it is printing on next page. Please don't confuse this question with others.

I have wrote a script which download output of text box. This script works fine on local server, it download the file successfully but when I put script on live server, the download file does not work. When download output button is clicked, instead of downloading file the output of page is shown on next page.

See live script here: http://www.globalitsoft.net/scripts/test.php

Here is the code of script:

<?php

    error_reporting(0);
    $mytext = "";

    $txt = preg_replace('/
+/', "
", trim($_POST['inputtext']));
    $text = explode("
", $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>
  • 写回答

1条回答 默认 最新

  • duanken7168 2017-05-19 13:18
    关注

    This works for me:

    $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;
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog
  • ¥15 Excel发现不可读取的内容