回答編集履歴

1

一部コードが抜けていたのを修正

2017/09/09 07:51

投稿

Tomak
Tomak

スコア1652

test CHANGED
@@ -20,17 +20,57 @@
20
20
 
21
21
 
22
22
 
23
+ HTMLの意味が違うかもしれませんが、下記PHPファイルを`sample.php`として保存してから実行すればそのまま同じディレクトリに`kadai.txt`が保存されるようになっています。
24
+
25
+
26
+
27
+ **※セキュリティなどは一切考慮していないのでこのままコピーして何かの本番業務に使用するのは危険です。**
28
+
29
+
30
+
23
31
  ```php
24
32
 
25
- $name = $_POST["name"];
33
+ <html>
26
34
 
27
- $comment = $_POST["comment"];
35
+ <head>
28
36
 
29
- $postedAt = date('Y-m-d H:i:s');
37
+ <title>sample</title>
30
38
 
39
+ </head>
40
+
41
+ <body>
42
+
43
+ <form action="./sample.php" method="post">
44
+
45
+ <table border="1">
46
+
31
- $count = 1;
47
+ <tr>
48
+
49
+ <td>名前</td><td><input type="text" name="name" value="<?php echo isset($_POST['name']) ? $_POST["name"] : ''; ?>"></td>
50
+
51
+ </tr>
52
+
53
+ <tr>
54
+
55
+ <td>コメント</td><td><input type="text" name="comment" value="<?php echo isset($_POST["comment"]) ? $_POST["comment"] : ''; ?>"></td>
56
+
57
+ </tr>
58
+
59
+ </table>
60
+
61
+ <br>
62
+
63
+ <input type="submit" name="create" value="新規投稿">
64
+
65
+ </form>
66
+
67
+ </body>
68
+
69
+ </html>
32
70
 
33
71
 
72
+
73
+ <?php
34
74
 
35
75
  //投稿管理ファイル
36
76
 
@@ -40,38 +80,90 @@
40
80
 
41
81
 
42
82
 
43
- //最終行から投稿番号取得
83
+ //投稿時間
44
84
 
45
- fseek($fp, -1, SEEK_END);
85
+ $postedAt = date('Y-m-d H:i:s');
46
86
 
47
- $end = fgets($fp);
87
+ //投稿番号
48
88
 
49
-
50
-
51
- if (! empty($end)) {
52
-
53
- $end = str_replace(['{','}'], ['',''], $end);
54
-
55
- $end = explode("\t<>\t", $end);
56
-
57
- if (count($end) > 0) {
89
+ $count = 1;
58
-
59
- $count = intval($end[0]) + 1; //最新の投稿番号
60
-
61
- }
62
-
63
- }
64
90
 
65
91
 
66
92
 
67
93
  //新規投稿
68
94
 
95
+ if (isset($_POST['create'])) {
96
+
69
- $newData = "{".$count."}"."\t<>\t". "{".$name."}"."\t<>\t"."{".$comment."}"."\t<>\t"."{".$postedAt."}"."\n";
97
+ if (empty($_POST['name']) || empty($_POST["comment"])) {
98
+
99
+ exit();
100
+
101
+ }
70
102
 
71
103
 
72
104
 
73
- fwrite($fp, $newData);
105
+ $name = $_POST["name"];
74
106
 
107
+ $comment = $_POST["comment"];
108
+
109
+
110
+
111
+ //最終行にカーソル(ファイルポインタ)移動
112
+
113
+ $offset = -2;
114
+
115
+ while (fgetc($fp) !== "\n") {
116
+
117
+ fseek($fp, $offset--, SEEK_END);
118
+
119
+ }
120
+
121
+ //下記でも同じ(最終行にカーソル(ファイルポインタ)移動)
122
+
123
+ //for ($offset=-2, fseek($fp, $offset, SEEK_END); fgetc($fp) !== "\n"; $offset--) {
124
+
125
+ // fseek($fp, $offset, SEEK_END);
126
+
127
+ //}
128
+
129
+
130
+
131
+ //最終行取得
132
+
133
+ $end = fgets($fp);
134
+
135
+
136
+
137
+ if (! empty($end)) {
138
+
139
+ $end = str_replace(['{','}'], ['',''], $end);
140
+
141
+ $end = explode("\t<>\t", $end);
142
+
143
+ if (count($end) > 0) {
144
+
145
+ $count = intval($end[0]) + 1; //最新の投稿番号
146
+
147
+ }
148
+
149
+ }
150
+
151
+
152
+
153
+ //新規投稿保存フォーマット
154
+
155
+ $newData = "{".$count."}"."\t<>\t". "{".$name."}"."\t<>\t"."{".$comment."}"."\t<>\t"."{".$postedAt."}"."\n";
156
+
157
+
158
+
159
+ fwrite($fp, $newData);
160
+
75
- fclose($fp);
161
+ fclose($fp);
162
+
163
+ }
164
+
165
+ ?>
76
166
 
77
167
  ```
168
+
169
+