Skip to content

Commit 82af002

Browse files
committed
upload multiple files
1 parent 836e759 commit 82af002

File tree

3 files changed

+48
-0
lines changed

3 files changed

+48
-0
lines changed

05-upload-multiple-files/.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
assets

05-upload-multiple-files/form.html

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8">
5+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
6+
<title>Upload Multiple Files PHP | Jago Ngoding</title>
7+
</head>
8+
<body>
9+
<h1>Upload Multiple Files PHP | Jago Ngoding</h1>
10+
<form action="proses.php" method="POST" enctype="multipart/form-data">
11+
<div>
12+
<label>Pilih gambar</label> <br>
13+
<input type="file" name="listGambar[]" accept="image/*" multiple>
14+
</div>
15+
16+
<button style="margin-top: 2rem">Upload</button>
17+
</form>
18+
</body>
19+
</html>

05-upload-multiple-files/proses.php

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
$folderUpload = "./assets/uploads";
4+
5+
# periksa apakah folder tersedia
6+
if (!is_dir($folderUpload)) {
7+
# jika tidak maka folder harus dibuat terlebih dahulu
8+
mkdir($folderUpload, 0777, $rekursif = true);
9+
}
10+
11+
$files = $_FILES;
12+
$jumlahFile = count($files['listGambar']['name']);
13+
14+
for ($i = 0; $i < $jumlahFile; $i++) {
15+
$namaFile = $files['listGambar']['name'][$i];
16+
$lokasiTmp = $files['listGambar']['tmp_name'][$i];
17+
18+
$namaBaru = uniqid() . '-' . $namaFile;
19+
$lokasiBaru = "{$folderUpload}/{$namaBaru}";
20+
$prosesUpload = move_uploaded_file($lokasiTmp, $lokasiBaru);
21+
22+
# jika proses berhasil
23+
if ($prosesUpload) {
24+
echo "Upload file <a href='{$lokasiBaru}' target='_blank'>{$namaBaru}</a> berhasil. <br>";
25+
} else {
26+
echo "<span style='color: red'>Upload file {$namaFile} gagal</span> <br>";
27+
}
28+
}

0 commit comments

Comments
 (0)