使用PHP创建下载文件而不保存到服务器上

使用PHP创建下载文件而不保存到服务器上,php,file,download,save,Php,File,Download,Save,假设我的PHP代码是 <?php // Get form variables using POST method $MarriagePostMortem=stripslashes($_POST['MarriagePostMortem']); $AutoSavesToKeep=stripslashes($_POST['AutoSavesToKeep']); // Open and write file $myFile="S3S.cfg"; $fh = fopen($myFile, 'w+

假设我的PHP代码是

<?php

// Get form variables using POST method
$MarriagePostMortem=stripslashes($_POST['MarriagePostMortem']);
$AutoSavesToKeep=stripslashes($_POST['AutoSavesToKeep']);

// Open and write file
$myFile="S3S.cfg";
$fh = fopen($myFile, 'w+') or die("can't open file");
fwrite($fh, "The Sims 3 Starter Configuration
-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-
MarriagePostMortem  $MarriagePostMortem
AutoSavesToKeep $AutoSavesToKeep
-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-");
fclose($fh); 

//echo "file written\n";
?>

这是你应该怎么做的,你的html标签应该是小写的&远离表格,除非你输出一个值表,使用divs和css。您还可以发回脚本,因为从表单收到的变量是特定的,您只需在那里检查正确的值,然后将数据发送回用户

<?php
if($_SERVER['REQUEST_METHOD']=='POST'){

    $mpm = (!empty($_POST['mpm']) && ($_POST['mpm']=='true' || $_POST['mpm']=='false'))?$_POST['mpm']:die('Wrong parameter type');
    $astk = (!empty($_POST['astk']) && is_numeric($_POST['astk']))?$_POST['astk']:die('Wrong parameter type');


    $content  = "The Sims 3 Starter Configuration".PHP_EOL;
    $content .= "-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-".PHP_EOL;
    $content .= "MarriagePostMortem  $mpm".PHP_EOL;
    $content .= "AutoSavesToKeep $astk".PHP_EOL;
    $content .= "-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-".PHP_EOL;

    header('Content-Description: File Transfer');
    header('Content-Type: application/octet-stream');
    header('Content-disposition: attachment; filename=S3S.cfg');
    header('Content-Length: '.strlen($content));
    header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
    header('Expires: 0');
    header('Pragma: public');
    echo $content;
    exit;
}
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Sims 3 Starter Configuration</title>
</head>

<body>

<h1>Sims 3 Starter Configuration</h1>

<form action="" method="post">
<input type="reset" value="Clear changes">
<br />  
<p>marriagepostmortem
  <select name="mpm">
    <option value="true">enabled</option>
    <option value="false" selected="selected">disabled</option>
  </select>
  Keep marriage lines on family tree entries after death. experimental!</p>

<p>autosavestokeep
  <select name="astk">
    <option value="1">1</option>
    <option value="2" selected="selected">2</option>
    <option value="3">3</option>
    <option value="4">4</option>
    <option value="5">5</option>
    <option value="6">6</option>
    <option value="7">7</option>
    <option value="8">8</option>
    <option value="9">9</option>
    <option value="10">10</option>
  </select>
  Number of autosaves to keep.</p>

<p><input type="submit" value="Generate"></p>
</form>
<p>Drag the file onto the sims 3 starter to add configurations.</p>
</body>
</html>

模拟人生3起动器配置
模拟人生3起动器配置

结婚仪式 启用 残废 在死后的家谱条目上保留婚姻线。实验性的

自动存储 1. 2. 3. 4. 5. 6. 7. 8. 9 10 要保留的自动保存数

将该文件拖到sims 3 starter上以添加配置


我现在收到如下错误消息:@Kuuchuu您不能发送两次头。查看Lawrence Cherone的答案。这是一种比Dagon的答案更正确的方法,这个答案还包括解决重新加载页面时遇到的标题问题。教一个人钓鱼…:-)我尽量不写剪切粘贴的答案,没有人从中学习
<?php
if($_SERVER['REQUEST_METHOD']=='POST'){

    $mpm = (!empty($_POST['mpm']) && ($_POST['mpm']=='true' || $_POST['mpm']=='false'))?$_POST['mpm']:die('Wrong parameter type');
    $astk = (!empty($_POST['astk']) && is_numeric($_POST['astk']))?$_POST['astk']:die('Wrong parameter type');


    $content  = "The Sims 3 Starter Configuration".PHP_EOL;
    $content .= "-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-".PHP_EOL;
    $content .= "MarriagePostMortem  $mpm".PHP_EOL;
    $content .= "AutoSavesToKeep $astk".PHP_EOL;
    $content .= "-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-".PHP_EOL;

    header('Content-Description: File Transfer');
    header('Content-Type: application/octet-stream');
    header('Content-disposition: attachment; filename=S3S.cfg');
    header('Content-Length: '.strlen($content));
    header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
    header('Expires: 0');
    header('Pragma: public');
    echo $content;
    exit;
}
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Sims 3 Starter Configuration</title>
</head>

<body>

<h1>Sims 3 Starter Configuration</h1>

<form action="" method="post">
<input type="reset" value="Clear changes">
<br />  
<p>marriagepostmortem
  <select name="mpm">
    <option value="true">enabled</option>
    <option value="false" selected="selected">disabled</option>
  </select>
  Keep marriage lines on family tree entries after death. experimental!</p>

<p>autosavestokeep
  <select name="astk">
    <option value="1">1</option>
    <option value="2" selected="selected">2</option>
    <option value="3">3</option>
    <option value="4">4</option>
    <option value="5">5</option>
    <option value="6">6</option>
    <option value="7">7</option>
    <option value="8">8</option>
    <option value="9">9</option>
    <option value="10">10</option>
  </select>
  Number of autosaves to keep.</p>

<p><input type="submit" value="Generate"></p>
</form>
<p>Drag the file onto the sims 3 starter to add configurations.</p>
</body>
</html>