2013-06-27 43 views
-1

好吧,我又回來了:),但帶來了一個新問題!

我試圖讓一個按鈕,點擊後,它會做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'> 
      <? 

謝謝你對任何人誰可以回答這個問題,我知道這是probubly一個簡單的問題,但我無法找到答案的任何地方,謝謝:)

+0

這裏'巫婆'的目的是什麼? – Daedalus

+0

' ...'? – Jaay

+0

no does not work ... – RepairServices

回答

3

<a>標籤有一個名爲location.href沒有這樣的屬性。你在考慮JavaScript。

使用href

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

nope不下載文件:( – RepairServices

+0

第一步:不要下載該文件,只需渲染您計算的'$ file'名稱。確保該文件存在,首先 – tadman

+0

我的文件確實存在! – RepairServices

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; ?> 
+0

不,我希望它去download.php沒有打開它,如在後臺,也下載.txt – RepairServices

+0

我還沒有測試你的下載腳本。它看起來不錯。你的下載應該在後臺開始。 – NeilB

2

你可以試試這個。

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