Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
创建要下载的文件,而无需下载到PHP服务器_Php_File_Download - Fatal编程技术网

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

创建要下载的文件,而无需下载到PHP服务器,php,file,download,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 '

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

<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
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;
$data='';
如果(isset($_POST['download'])){
标题('Content-disposition:attachment;filename=testing.txt');
标题(“内容类型:文本/普通”);
回声“同侧眼”;
退出();
}
$data.='
';
回波数据;

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

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

现在它将只加载createfile.php中的内容

只要将这一行
移动到
if
之后或
echo
内部,但是我如何指定我要显示的内容,比如说我是否只想显示存储在$content变量中的内容?而不是使用这个头方法,无论如何,我可以做同样的事情,这样我就不必在任何东西被回送之前放置代码了吗?你可以将你所有的网站html和css存储在一个变量中,并在最后回送出来?我会更新我的答案来展示一个例子。谢谢你的解决方案!无论如何,我已经想出了另一种方法,我会很快更新我的答案。。
<link rel="stylesheet" href="css/style.css" />

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