-
Notifications
You must be signed in to change notification settings - Fork 0
/
menulis-file.php
237 lines (217 loc) · 6.26 KB
/
menulis-file.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
<?php
function tulis() {
$namaFile = $_POST["namaFile"];
$isiFile = $_POST["isiFile"];
$pathFolder = "aset/file/";
// Validasi nama dan isi file
if(empty(trim($namaFile))) {
echo '<script>
alert("Nama file diperlukan");
</script>';
return;
}
if(empty(trim($isiFile))) {
echo '<script>
alert("File sebaiknya memiliki isi");
</script>';
return;
}
// Buat path folder jika belum ada
if(!file_exists($pathFolder)) {
mkdir($pathFolder, 0777, true);
}
$lokasiFile = $pathFolder . $namaFile . ".txt";
// Cek apakah file sudah ada
if(file_exists($lokasiFile)) {
echo '<script>
alert("Waduh, Nama File sudah tersedia, coba Nama File yang lain");
</script>';
return;
}
// Write jika file belum ada
if($file = fopen($lokasiFile, "x")) {
fwrite($file, $isiFile);
fclose($file);
echo '<script>
alert("Berhasil menambah file");
</script>';
return;
} else {
echo '<script>
alert("Gagal menambah file");
</script>';
return;
}
}
if(isset($_POST["tulis"])) {
tulis();
unset($_POST);
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Web Menulis File</title>
<style>
* {
margin: 0;
padding: 0;
box-sizing: border-box;
font-family: monospace;
}
.bapak {
min-height: 100vh;
display: grid;
grid-template-rows: auto 1fr;
}
.container {
display: grid;
grid-template-columns: auto 1fr;
height: 100%;
}
.header {
background-color: olivedrab;
padding: 2rem;
color: white;
text-align: center;
}
.sidebar {
background-color: darkslategray;
padding: 2rem;
}
.content {
padding: 2rem;
display: grid;
grid-template-rows: auto auto 1fr;
}
.footer {
background-color: darkseagreen;
padding: 2rem;
text-align: center;
}
.sidebar ul {
list-style: none;
display: flex;
flex-direction: column;
gap: 1rem;
}
.sidebar ul a {
background-color: teal;
display: block;
color: white;
text-decoration: none;
font-weight: bolder;
padding: 1rem 1.8rem;
border-radius: 1rem;
text-wrap: nowrap;
}
.sidebar ul a:hover {
background-color: teal;
}
.sidebar ul a:active {
filter: invert();
}
.main {
display: grid;
grid-template-rows: 1fr auto;
}
.active {
filter: brightness(80%);
}
form > * {
display: block;
width: 100%;
padding: 1rem;
}
form button {
margin-top: 2rem;
}
.daftar-file {
margin-top: 2rem;
background-color: lightsalmon;
border: 1px solid darkslategray;
border-radius: 0.6rem;
padding: 1rem;
display: grid;
grid-template-columns: repeat(auto-fill, minmax(16rem, 1fr));
gap: 1rem;
}
.daftar-file > * {
background-color: lightyellow;
padding: 1rem;
border-radius: 0.5rem;
height: fit-content;
}
.daftar-file-label {
padding-top: 2rem;
text-align: center;
}
</style>
</head>
<body>
<div class="bapak">
<div class="header">
<h2>Krisna no Hikari</h2>
</div>
<div class="container">
<div class="sidebar">
<ul>
<li>
<h3>
<a href="/">Halaman Utama</a>
</h3>
</li>
<li>
<h3>
<a href="menulis-file.php" class="active">Menulis File</a>
</h3>
</li>
<li>
<h3>
<a href="membaca-file.php">Membaca File</a>
</h3>
</li>
<li>
<h3>
<a href="mengurutkan.php">Mengurutkan</a>
</h3>
</li>
</ul>
</div>
<div class="main">
<div class="content">
<form action="" method="post">
<label for="namaFile">Nama File</label>
<input type="text" name="namaFile" id="text" placeholder="Nama File">
<label for="isiFile">Isi File</label>
<textarea name="isiFile" id="isiFile" rows="5" placeholder="Isi File"></textarea>
<button type="submit" name="tulis">Tambah</button>
</form>
<h3 class="daftar-file-label">File yang sudah ada</h3>
<div class="daftar-file">
<?php
if(!file_exists("aset/file/")) {
mkdir("aset/file/", 0777, true);
}
foreach(glob("aset/file/*") as $file) :
?>
<p><?= basename(pathinfo($file, PATHINFO_FILENAME)) ?></p>
<?php endforeach; ?>
</div>
</div>
<div class="footer">
<h3>Tidak © Copyright</h3>
</div>
</div>
</div>
</div>
<script>
// Saat refresh tidak submit - StackOverflow
if (window.history.replaceState) {
window.history.replaceState(null, null, window.location.href);
}
</script>
</body>
</html>