摘要:<!DOCTYPE html> <html> <head> <
<!DOCTYPE html>
<html>
<head>
<title>create 'txt' file</title>
<style type="text/css">
*{margin:0;padding: 0;}
div{margin: 30px auto;width: 400px;}
input{margin: 0 10px;}
textarea{width: 400px;height:200px; resize: none;}
</style>
</head>
<body>
<div>
<form action="samFile.php" method="post">
<p>請(qǐng)輸入文件名:<input type="text" name="fileName"></p>
<p>請(qǐng)輸入文件內(nèi)容:</p>
<p> <textarea name="content"></textarea> </p>
<input type="submit" name="submit">
</form>
</div>
<hr>
<div>
<p class="fileName"></p>
<p class="fileContent"></p>
</div>
</body>
<script>
var fileName=document.getElementsByClassName('fileName').item(0);
var fileContent= document.getElementsByClassName('fileContent').item(0);
</script>
<?php
//獲取請(qǐng)求的文件名
@$fileName = $_REQUEST['fileName'];
//獲取文件內(nèi)容
@$content = $_REQUEST['content'];
//未填文件名,不操作
if(!empty($fileName)){
//為文件名加入反綴名
$fileName=$fileName.'.txt';
//創(chuàng)建一個(gè)文本文件
if(!file_exists($fileName)){
touch($fileName);
}
//打開(kāi)文本文件回一個(gè)資源
$file = fopen($fileName,'a+');
//寫(xiě)入內(nèi)容
fwrite($file,$content."\r\n");
//聲明一個(gè)空數(shù)組,存入字符串
$fileStr=[];
//文件指針到開(kāi)頭
rewind($file);
//讀取文件內(nèi)容到數(shù)組
while (!feof($file)) {
//$fileStr[] = fgets($file);
$fileStr[]=fgets($file);
}
//數(shù)組轉(zhuǎn)字符串變量
$str=implode($fileStr);
//去掉字符串的換行符
$str = preg_replace('/\n|\r\n/','<br>',$str);
//內(nèi)容顯示到頁(yè)面上
echo "<script>fileName.innerHTML='文件名為: {$fileName}';fileContent.innerHTML='{$str}';</script>";
}
?>
</html>
批改老師:天蓬老師批改時(shí)間:2019-10-12 16:29:02
老師總結(jié):OK, 趕緊學(xué)習(xí)剩下 的課程吧, 加油