-2

我有一个带有表格的站点,并希望在每个tr. 单击按钮应弹出下载对话框(保存文件),然后按“保存”,下载文件。

我经常看到这种情况,但仍然不知道该怎么做。我已经通过表单设置了按钮:

<form method="get">
<input type="submit" value="<?php echo $paket; ?>" name="download" />
</form>

where$paket包含没有.zip结尾的文件名。

在同一个 php 文件中,就在两个require_once语句下,我这样做了:

if ( isset( $_GET['download'] ) ) {
$name = $_GET['download'];
header( 'Content-Disposition: attachment; filename = '.$name.'.zip' );
header( 'Content-type: application/zip' );
}

如果我按下按钮,我会收到以下错误:

Warning: Cannot modify header information - headers already sent by (output started at /is/lib/require.req:154) 

我知道标题信息需要放在最上面,但是require_once如果require_once后面几行,我应该如何使用我包含的函数?

有没有我找不到的不错(且万无一失)的教程?

4

4 回答 4

1

Use an output buffer if you need to send content before the headers http://php.net/manual/pt_BR/function.ob-start.php . Anyway your donwload file should be on a different location or put the download handling part at the very top off the file and exit after it

于 2012-08-09T14:47:26.737 回答
0

不要使用echo,print()或类似的东西之前header()or start_session()。HTTP 特定信息必须在公共输出之前。

只需查看php-documentation

"Remember that header() must be called before any actual output is sent [...]"
于 2012-08-09T14:50:21.967 回答
0

必须始终在任何其他输出发生之前发送标头。这意味着它必须是当前文件(包含标头信息)中发送的第一件事,但它也必须是包含此文件的所有其他文件的第一件事。

不会工作

<?php
    // application logic here
    print "X";
    require 'header.php';
    // application logic here

<?php
    // application logic here
    require 'header.php';
    // application logic here

头文件.php

<?php
    // application logic here
    header( 'Content-type: application/zip' );
    // application logic here
于 2012-08-09T14:50:31.540 回答
0

原来在第一个<?php标签之前有一个空格,导致了错误。

于 2012-12-07T14:48:51.577 回答