[英]Get user input to php file
我對php很陌生,只是開始從互聯網學習。 我看過一些文件處理的例子。 但是,當我按照相同的步驟進行文件寫入時,它不起作用。
讀取功能是工作文件。 但是寫入文件不起作用。 我也嘗試過使用file_put_content函數來編寫。 :(
<?php
if(isset($_POST['submit1'])){
$fileName = "Text.txt";
$fh = fopen($fileName,"a") or die("can not open the file to write");
//Writing to a file
$newData = "Hello This is new Data";
fwrite($fh,$newData);
fclose($fh);
//Reading a file -- Working
$text = fopen("Text.txt","r") or die ("can not open the file to read");
while(!feof($text))
{
$myLine = fgets($text);
print $myLine;
}
fclose($text);
}
?>
請引導我..謝謝
這可以正常工作,您會得到什么錯誤?
<?php
$file = 'text.txt';
$writer = fopen($file, 'a');
$addData = 'This is a new string to be added at the end of the file';
fwrite($writer, $addData);
fclose($writer);
?>
EDIT1:要輸入POST請求的輸入,您可以執行以下操作:
<?php
if ($_SERVER['REQUEST_METHOD'] == 'POST'){
$addData = $_POST['input-name'];
$file = 'text.txt';
$writer = fopen($file, 'a');
fwrite($writer, $addData);
fclose($writer);
}
?>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.