[英]Can't get my php to add to my text document?
我目前正在為一個大學項目工作,但是我遇到了問題。 我有兩個頁面,每個頁面上都有一個表單,其中包括三個文本字段(des,act,date),我試圖將其添加到文本文檔中,以便將表單中的信息添加到文本文檔中,但是在此過程中卻一刻不做正在覆蓋它。 有人知道如何解決嗎?
第1頁
if (isset($_GET['logout'])){
session_destroy();
}
if (!isset($_SESSION['loggedin']) || $_SESSION['loggedin'] == false) {
header("Location: index.php");
}
//Send Data
$content = 'OBSERVATION'."\r\n".'Breif Description: '.$_POST['des1']."\r\n".'Agreed Action: '.$_POST['act1']."\r\n".'Close Date: '.$_POST['date1']."\r\n";
if (isset($_POST['submit'])){
$myFile=fopen("Observation.txt","w") or exit("Can’t open file!");
fwrite($myFile, $content);
fclose($myFile);
header( 'Location: http://www.murphy.sulmaxmarketing.com/GoodPractices.php' ) ;
}
?>
第2頁
if (isset($_GET['logout'])){
session_destroy();
}
if (!isset($_SESSION['loggedin']) || $_SESSION['loggedin'] == false) {
header("Location: index.php");
}
//Send Data
$content = "\r\n\r\n".'GOOD PRACTICES'."\r\n".'Breif Description: '.$_POST['des2']."\r\n".'Agreed Action: '.$_POST['act2']."\r\n".'Close Date: '.$_POST['date2']."\r\n";
if (isset($_POST['submit'])){
$myFile=fopen("Observation.txt","w") or exit("Can’t open file!");
fwrite($myFile, $content);
fclose($myFile);
}
?>
具有'w'
模式的fopen()
僅開放寫作; 將文件指針放在文件的開頭,並將文件截斷為零長度 。 如果該文件不存在,請嘗試創建它。
具有'a'
模式的fopen()
僅開放寫作; 將文件指針指向文件的結尾 。 如果該文件不存在,請嘗試創建它。 在這種模式下,fseek()無效, 寫操作總是追加 。
使用帶有FILE_APPEND
標志的file_put_contents
函數。
此函數與依次調用fopen(),fwrite()和fclose()來將數據寫入文件相同。
FILE_APPEND :如果文件名已經存在,請將數據附加到文件中而不是覆蓋它。
...
if (isset($_POST['submit'])) {
file_put_contents("Observation.txt", $content, FILE_APPEND);
header( 'Location: http://www.murphy.sulmaxmarketing.com/GoodPractices.php' ) ;
exit;
}
...
使用file_put_content
if (isset($_POST['submit'])) {
file_put_contents("Observation.txt", $content, FILE_APPEND);
... your code here
}
在這里,file_put_content“ FILE_APPEND”中的第三個參數每次都會在您之前的代碼中附加新內容,因為名稱相同,它會將一個內容覆蓋為另一個內容,因此,如果要以這種方式進行操作,而不是設置其他名稱兩個文件。
在這里file_put_content函數的URL: http : //php.net/manual/en/function.file-put-contents.php
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.