创建要下载的文件而无需下载到服务器PHP

时间:2013-08-01 20:14:26

标签: php file download

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

<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文件现在包含<?php之前的内容<link rel="stylesheet" href="css/style.css" />以及Lorem Ipsum,但我只想要我指定的内容Lorem Ipsum < / p>

2 个答案:

答案 0 :(得分: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;

答案 1 :(得分: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中加载东西