使用$ _POST下载文件(PHP和HTML)

时间:2013-06-27 07:57:46

标签: php html

好的,我又回来了:),但有一个新问题!

我正在尝试创建一个按钮,点击后,它会执行location.href到我的download.php

巫婆的代码是:

<?
ob_start();
require_once 'includes/db.php';
require_once 'includes/init.php';
?>
<?php

     $file = "logs/".$_SESSION['username'].".txt";

if (file_exists($file)) {
    header('Content-Description: File Transfer');
    header('Content-Type: application/octet-stream');
    header('Content-Disposition: attachment; filename='.basename($file));
    header('Content-Transfer-Encoding: binary');
    header('Expires: 0');
    header('Cache-Control: must-revalidate');
    header('Pragma: public');
    header('Content-Length: ' . filesize($file));
    ob_clean();
    flush();
    readfile($file);
    exit;
}
?>

我的其他代码使用初始化按钮来下载.txt文件:

<?php 
        if (isset($_POST['clearBtn']))
        {
             ?>
               < location.href = 'download.php'>
              <?
            echo '<div class="nNote nSuccess hideit"><p><strong>SUCCESS: </strong>Logs have been downloaded</p></div>';
        }
        ?>
        <div class="widget">
          <div class="title"><img src="images/icons/dark/frames.png" alt="" class="titleIcon" /><h6>Logs</h6><form action = "" method="post" class="form">
          <input type="submit" style="margin-top: 4px; margin-right:4px;" value="Download Logs" name="clearBtn" class="dblueB logMeIn" /> 

如何使这部分工作:

          ?>
          < location.href = 'download.php'>
          <?

感谢任何能够回答此问题的人,我知道这可能是一个简单的问题,但我找不到任何答案,谢谢:)

3 个答案:

答案 0 :(得分:3)

<a>标记没有名为location.href的此类属性。你在想javascript。

使用href

<a href="download.php">Text</a>

答案 1 :(得分:2)

你可以试试这个。

<a href="Javascript:void(0)" onclick="window.location.href='download.php'"> Text </a>

答案 2 :(得分:1)

要自动下载文件,请执行以下操作:

<?php if (isset($_POST['clearBtn'])): ?>
  <script type="text/javascript">window.location.href='download.php';</script>
  <div class="nNote nSuccess hideit"><p><strong>SUCCESS: </strong>Logs have been downloaded</p></div>
<?php endif; ?>