2013-01-11 63 views
1

我現在已經繞過Jquery,但希望將文本區域保存到txt文件。最終的目標是能夠將文本文件上傳到同一個地方。我在代碼中缺少什麼?PHP不會將文本區域發送到文件

不知道我在哪裏定義了這個代碼中的textarea,或者它是否應該在另一個文件中,或者如果html文件應該有php的後綴?

下面的歡呼是我在PHP的嘗試。

CODE

<?php 
    if(isset($_POST['submit_save'])) { 
     $file = "output.txt"; 
     $output = $_POST['output_str']; 
     file_put_contents($file, $output); 
     $text = file_get_contents($file); 

     header("Content-type: application/text"); 
     header("Content-Disposition: attachment; filename=\"$file\""); 
     echo $text; 
    } 
    else 
    {  
     $_POST['output_str'] = ""; 
    } 
?> 

</head> 

<body> 
    <input id="submit_save" type="button" value="save" /> 
    </br></br></br> 
    <div id="opt"></div> 
</body> 
</html> 
+2

是什麼問題?該文件沒有創建?創建的文件是否爲空? (jQuery)代碼調用該文件在哪裏? – yankee

+0

另外,'textarea'在哪裏?和'form'標籤? – Carsten

+0

@yankee對不起,忘了把它放在我期待的保存文件對話上來,但我什麼也沒有得到 – Ferg

回答

2

$_POST是名稱。您需要添加名字#submit_save

<input name="submit_save" id="submit_save" type="button" value="save" /> 
0

使用ReadFile函數和不要忘記「退出」不破壞文件之後。像Enve said,POST數組鍵是html「name」。

下面的例子從手冊頁:

<?php 
$file = 'monkey.gif'; 

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; 
} 

來源:http://php.net/manual/en/function.readfile.php

0

你的PHP腳本在您的Web服務器上運行,而不是在瀏覽器中。腳本的結果是瀏覽器收到的結果!

在讀你的問題(S),目前尚不清楚(我)你是否想(文本區域)輸入的數據發送到瀏覽器作爲文件或作爲HTML頁面。你似乎想要兩種,這是不可能的,選擇一種。

Fab Sa已經演示瞭如何發送文件到PHP的瀏覽器,所以我不會再討論這個。

要在在HTML頁面文本區域中輸入的數據填寫,您必須在HTML <form>添加<textarea> - 標籤。事情是這樣的:

<form method="post" action="<?= $PHP_SELF ?>"> 
<textarea name="output_str"><?= $_POST['output_str'] ?></textarea> 
<input id="submit_save" name="submit_save" type="button" value="save" /> 
</form> 

而且刪除你的這部分代碼(該部分將數據發送的文件):

$text = file_get_contents($file); 
    header("Content-type: application/text"); 
    header("Content-Disposition: attachment; filename=\"$file\""); 
    echo $text; 

注意:出於安全考慮,您應該從未使用$_POST['output_str']沒有sanitizing它(我在示例中忽略了它)。

+0

謝謝我認爲我在我的頭上用PHP我希望textarea保存爲一個文件,用戶可以先下載。那麼我會嘗試解決上傳部分。 – Ferg

0

我認爲你正在嘗試這樣的事情。

<?php 

    if(isset($_POST['textfield'])) { 
    $file = "output.txt"; 
    $output = htmlspecialchars ($_POST['textfield']); 
    file_put_contents($file, $output); 
    $text = file_get_contents($file); 

    header("Content-type: application/text"); 
    header("Content-Disposition: attachment; filename=\"$file\""); 
    echo $text; 
    exit; 

    } 
?> 

<html> 
<head> 
    <title>Your app</title> 
</head> 

<body> 
    <form method="POST"> 
    <textarea name="textfield"></textarea> 
    <input id="submit_save" type="button" value="save" /> 
    </form> 

</body> 
</html> 
+0

謝謝,但它似乎並不適用於我可以我的文本區域有一個ID和名稱? – Ferg

+0

是的,你可以給textarea一個單獨的「id」和「name」。 表單使用了名稱,如果我沒有錯,則DOM使用id。 –

相關問題