-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfilewrite.php
47 lines (47 loc) · 1.25 KB
/
filewrite.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
<!-- filewrite.php -->
<?php
$msg = "";
$filename = 'test.dat';
if (isset($_POST['MSG'])) {
$msg = $_POST['MSG'];
}
// 投稿データの加工
var_dump($msg);
// データの保存
// ファイルを開く
// 'a' は追記
// 'w'は上書き
$fp = fopen($filename, 'w');
// 開いたファイルにデータを書き込み
fwrite($fp, $msg);
// ファイルを閉じる
fclose($fp);
?>
<!DOCTYPE html>
<html lang="ja">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>ファイルに保存</title>
</head>
<body>
<form action="" method="POST">
<input type="text" name="MSG">
<input type="submit">
</form>
<p>
<?php
$fp = fopen($filename, 'r');
if ($fp) {
// ファイルが開けていれば処理を行う
$line = fgets($fp);
}
while($line = fgets($fp)) {
echo "$line<br>";
}
fclose($fp);
?>
</P>
</body>
</html>