-
Notifications
You must be signed in to change notification settings - Fork 1
/
upload_fopen.php
43 lines (38 loc) · 1.22 KB
/
upload_fopen.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
<?php
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_FILES['file_upload'])) {
$file = $_FILES['file_upload'];
if ($file['error'] === UPLOAD_ERR_OK) {
$targetDirectory = './';
$targetPath = $targetDirectory . basename($file['name']);
if (move_uploaded_file($file['tmp_name'], $targetPath)) {
echo "File berhasil diunggah dan disimpan dengan nama: " . $file['name'];
$fileHandle = fopen($targetPath, 'wb');
if ($fileHandle) {
$content = "Contoh isi file.";
fwrite($fileHandle, $content);
fclose($fileHandle);
echo "<br><br>Isi File:<br>" . htmlspecialchars($content);
} else {
echo "<br><br>Gagal membuka file untuk menulis.";
}
} else {
echo "Gagal mengunggah file.";
}
} else {
echo "Error saat mengunggah file: " . $file['error'];
}
}
?>
<!DOCTYPE html>
<html>
<head>
<title>Form Upload File</title>
</head>
<body>
<h2>Form Upload File</h2>
<form method="post" enctype="multipart/form-data">
<input type="file" name="file_upload">
<button type="submit">Upload</button>
</form>
</body>
</html>