Repository navigation
Expand file tree
/
Copy pathpractice9.php
More file actions
55 lines (51 loc) · 2.04 KB
/
Copy pathpractice9.php
File metadata and controls
55 lines (51 loc) · 2.04 KB
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
48
49
50
51
52
53
54
55
<?php
$message = '';
if ($_SERVER["REQUEST_METHOD"] == "POST") {
if (isset($_POST["content"]) && !empty($_POST["content"])) {
$content = $_POST["content"];
$dir = 'output';
$file = $dir . '/output.txt';
// ディレクトリが存在しない場合は作成させるぞ!!!
if (!is_dir($dir)) {
if (!mkdir($dir, 0777, true)) {
$message = "ディレクトリの作成に失敗しました。";
}
}
/*
つまり、is_dirはディレクトリが存在する状態を表している関数で、!でそれを否定形にする。
is_dir関数はその後に()の中に関数名を当てはめることでそのディレクトリが存在する状態を表すことができる
なので!is_dir($dir)で$dirに当てはまる名前のディレクトリが無い状態を表し、それをifで囲うことで、もし$dirの名前のディレクトリがなかった場合の挙動を記述している
*/
// ファイルに書き込みできるかな...?
//できたどおおお
if (empty($message)) {
if (file_put_contents($file, $content . PHP_EOL, FILE_APPEND) !== false) {
$message = "ファイルに書き込みました: " . htmlspecialchars($content);
} else {
$message = "ファイルの書き込みに失敗しました。";
}
}
} else {
$message = "内容を入力してください。";
}
} elseif ($_SERVER["REQUEST_METHOD"] != "GET") {
$message = "無効なリクエストです。";
}
?>
<!DOCTYPE html>
<html lang="ja">
<head>
<meta charset="UTF-8">
<title>フォーム送信</title>
</head>
<body>
<?php if ($message): ?>
<p><?php echo $message; ?></p>
<?php endif; ?>
<form method="POST" action="">
<label for="content">内容:</label>
<input type="text" id="content" name="content" required>
<button type="submit">送信</button>
</form>
</body>
</html>