无法修改标题信息 - 下载文件

时间:2014-10-27 10:13:46

标签: php html forms

我正在努力解决这个问题。

我在页面上创建两个表单以生成设备的配置文件,我们使用的两个不同设备中的每一个都有一个表单。表单工作正常并正确提交值,但我需要该文件来启动下载。使用一个表单我已经能够实现这一点,但有两种形式我得到关于标题信息的错误。

这是我文件的头部

<html>
<head>
  <link rel="stylesheet" type="text/css" href="screen.css" />
  <?php 
      include('header.php'); 
      include('config/wan.php');
      include('config/wan-voip.php');
      include('config/pppoe.php');
      include('config/pppoe-voip.php');
      include('config/dlink.php');
   ?>
 </head>
<body>

这个si后跟一个标准的HTML表单,然后这是我的代码来验证输入并采取相应的行动:

if(isset($_POST['submit']))
{

    if(!empty($_POST['type']) && isset($_POST['router']) && $_POST['router'] == 'netgear')
    {
        if($_POST['type'] == 'pppoe' && (empty($_POST['username']) || empty($_POST['password'])))
        {
            echo 'Please fill in the username and password';
            exit;
        }



        if(empty($_POST['ssid']) || empty($_POST['pass']))
        {
            echo 'Please provide the SSID and Wireless Key';
            exit;
        }

        if(($_POST['type'] == 'wan-voip' || $_POST['type'] == 'pppoe-voip') && (empty($_POST['ext']) || empty($_POST['extpass'])))
        {
            echo 'Please provide the Extension Information';
            exit;
        }
    }
    elseif(isset($_POST['router']) && $_POST['router'] == 'dlink')
    {
        if(empty($_POST['ssid']) || empty($_POST['pass']))
        {
            echo 'Please provide the SSID and Wireless Key';
            exit;
        }
    }


    ob_end_clean();

    if($_POST['router'] == 'netgear')
    {
        header("Content-type: text/plain");
        header("Content-Disposition: attachment; filename=".date('d-m-Y')."-".$_POST['type'].".ini");

        $file = str_replace('-', '', $_POST['type']); 
        echo $$file;
    }
    elseif($_POST['router'] == 'dlink')
    {
        header("Content-type: text/plain");
        header("Content-Disposition: attachment; filename=".date('d-m-Y')."-dlink.cfg");
        echo $dlink;
    }



}

有人可以建议我如何解决这个问题。我已经阅读了一些其他相关的地方,并尝试移动脚本的位置并尝试一些建议,但我似乎一直在打砖墙。

1 个答案:

答案 0 :(得分:2)

在http协议中,标题应始终在正文之前发送。

这就是您收到此错误的原因,您正在使用

header("Content-type: text/plain");

在您输出了一些文字后。

如果您想使用header方法,则必须在任何“回声”之前执行此操作。或任何文字输出