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

报告相同问题?

悬赏问题

  • ¥15 运筹学排序问题中的在线排序
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥30 求一段fortran代码用IVF编译运行的结果
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥30 python代码,帮调试,帮帮忙吧
  • ¥15 #MATLAB仿真#车辆换道路径规划