PHP下载按钮无法工作生成空文件 [英] PHP Download Button Not Working Generate Empty File

查看:108
本文介绍了PHP下载按钮无法工作生成空文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在表单中有两个按钮,一个是提交,第二个按钮下载textarea中显示的输出。

I have two button in the form, one is to submit and second button download the output shown in the textarea.

提交工作正常,但下载按钮创建空文件并且不写输出文本区域的数据。

The submit works fine but download button create empty file and does not write data of output text area.

这是代码:

Here is code:

<?php    
error_reporting(0);
$mytext = "";

$txt = preg_replace('/\n+/', "\n", trim($_POST['inputtext']));
$text = explode("\n", $txt);
$output  = array();


if(isset($_POST["submit"]))
{
    for($i=0;$i<count($text);$i++)
    {
        $output[] .= trim($text[$i]) . ' Text added with output'.PHP_EOL;
    }
}


if(isset($_POST["download"]) ) {

    $handle = fopen("file.txt", "w");
    fwrite($handle, implode($output));
    fclose($handle);

    header('Content-type: application/octet-stream');
    header('Content-Disposition: attachment;filename='.basename('file.txt'));
    header('Expires: 0');
    ob_end_clean();
    header('Cache-Control: must-revalidate');
    header('Pragma: public');
    header('Content-Length: ' . filesize('file.txt'));
    readfile('file.txt');
    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>


推荐答案

你从第二个textarea取得它),所以使用 if(isset($ _ POST [submit])|| isset($ _ POST [download])) in测试,而不是只有 if(isset($ _ POST [submit]))

You have to generate the output also if downloading (unless you take it from the second textarea), so use if(isset($_POST["submit"]) || isset($_POST["download"]) ) in the test, instead of only if(isset($_POST["submit"]))

最终代码看起来像这样:

The final code would look like this:

<?php

error_reporting(0);
$mytext = "";

$txt = preg_replace('/\n+/', "\n", trim($_POST['inputtext']));
$text = explode("\n", $txt);
$output  = array();


/* CHANGED HERE */

if(isset($_POST["submit"]) || isset($_POST["download"]) ) {


    for($i=0;$i<count($text);$i++) {

        $output[] .= trim($text[$i]) . ' Text added with output'.PHP_EOL;

    }

}


if(isset($_POST["download"]) ) {

    $handle = fopen("file.txt", "w");
    fwrite($handle, implode("\r",$output));
    fclose($handle);

    header('Content-type: application/octet-stream');
    header('Content-Disposition: attachment; filename='.basename('file.txt'));
    header('Expires: 0');
    ob_end_clean();
    header('Cache-Control: must-revalidate');
    header('Pragma: public');
    header('Content-Length: ' . filesize('file.txt'));
    readfile('file.txt');
    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>

这篇关于PHP下载按钮无法工作生成空文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆