-1

我要做的是生成一个文件并在其中指定内容,而无需在服务器上实际保存和创建文件。

<link rel="stylesheet" href="css/style.css" />
<?php
if(isset($_POST['download'])){
    header('Content-disposition: attachment; filename=testing.txt');
    header('Content-type: text/plain');
    echo 'Lorem Ipsum';
    exit();
}
echo 
'
<form method="POST" action="index.php">
<input type="submit" name="download" value="submit"/>
</form>
';
?>

生成的 .txt 文件现在包含<?phpwhic is <link rel="stylesheet" href="css/style.css" />and also之前的内容Lorem Ipsum,但我只想要我指定的内容,这只是Lorem Ipsum

4

2 回答 2

1

标头应该在任何内容被回显之前出现。

<?php
if(isset($_POST['download'])){
    header('Content-disposition: attachment; filename=testing.txt');
    header('Content-type: text/plain');
    echo 'Lorem Ipsum';
    exit();
}
echo 
'
<link rel="stylesheet" href="css/style.css" />
<form method="POST" action="index.php.php">
<input type="submit" name="download" value="submit"/>
</form>
';
?>

要将其保持在相同的结构中:

$data = '<link rel="stylesheet" href="css/style.css" />';

if(isset($_POST['download'])){
    header('Content-disposition: attachment; filename=testing.txt');
    header('Content-type: text/plain');
    echo 'Lorem Ipsum';
    exit();
}
$data .='
<form method="POST" action="index.php.php">
<input type="submit" name="download" value="submit"/>
</form>
';

echo $data;
于 2013-08-01T20:17:22.560 回答
0

我创建了另一个名为createfile.php

<?php
     header('Content-diposition: attachment; filename=testing.php');
     header('Content-type: text/plain');
     echo 'Lorem Ipsum';
?>

并且在index.php

<link rel="stylesheet" href="css/style.css" />

<form action="createfile.php" method="POST">
    <input type="submit" name="submit" value="submit"/>
</form>

现在它只会在 createfile.php 中加载东西

于 2013-08-01T21:47:29.510 回答