2012-04-07 124 views
1

我有下面的php腳本,它檢查複選框元素的提交值並打印它們的值(如果選中)。我如何在php中創建一個文件並下載它

<?php 
echo '<table class="features-table">'; 

echo "<tbody>"; 
for ($i=1;$i<=2284;$i+=1) { 
    if($_POST[$i]) { 
    echo"<tr>"; 
      echo "<td><a href=http://www.m-w.com/dictionary/" . $_POST[$i] . ">" . $_POST[$i]. "</a></td>"; 
    echo "</tr>"; 
    } 
} 

?> 

我想使這個數據顯示給用戶,可供下載的文本文件。我應該如何創建下載按鈕。在這種情況下,我需要單獨的php腳本嗎?我將如何獲取提交給此php腳本的表單數據?

從谷歌搜索我得到了我需要使用類似的代碼如下

header("Content-type: text/plain"); 
header("Content-Disposition: attachment; filename=list.txt"); 
header("Pragma: no-cache"); 
header("Expires: 0"); 
echo 'data1,data2,data3...'; 

但我沒有得到我該如何與當前的PHP腳本集成它。

謝謝

回答

1

我沒有看到任何必要在飛行中創建文件。

我會做下列方式....

使可作爲隱藏字段中的數據,並封裝在一個表單標籤等領域的下載按鈕一起。當用戶單擊下載按鈕時,腳本中將提供所有表單數據。然後,您可以執行您想要對數據執行的任何操作。

+0

謝謝你給存儲在隱藏字段內容的想法! – vaichidrewar 2012-04-09 05:26:22

1

發佈的代碼可以由sudip他人幫助未來

的答案後,我做了以下變化

<?php 
$list = "Unknown words from GSL\n\n"; 
    echo '<table class="features-table">'; 
    echo "<tbody>"; 
    for ($i=1;$i<=2284;$i+=1) { 
     if($_POST[$i]) { 
     echo"<tr>"; 
       echo "<td><a href=http://www.m-w.com/dictionary/" . $_POST[$i] . ">" . $_POST[$i]. "</a></td>"; 
       $list = $list . $_POST[$i] . "\n"; 
     echo "</tr>"; 
     } 
    } 

echo' 
    <form action="download.php" method="post"> 
    <input type="hidden" name="wordlist" value="'. $list . '"> 

    <center><input type="submit" name="submit_parse" style="margin-bottom: 20px; margin-top: 10px;width:200px; font-face: "Comic Sans MS"; font-size: larger; " value=" Download as a text file "> </center> 
    </form> 
'; 
?> 

我不得不創建自頭單獨的PHP文件()必須在任何調用實際的輸出是通過普通的HTML標籤,文件中的空行或PHP發送的。

內容的download.php的

<?php 
header("Content-type: text/plain"); 
header("Content-Disposition: attachment; filename=list.txt"); 
header("Pragma: no-cache"); 
header("Expires: 0"); 
echo $_POST["wordlist"] ; 
?> 
+0

那麼,解決方法是你忘了在'download.php'的內容處添加''標籤? – 2016-09-02 05:59:18